2016-04-11 5 views
2

私は、私のrecyclerviewの中にチェックボックス、edittexts、および他の入力を持っています。私は正常にデータバインディングwhickを実装したデータは、Webサービスから取得したデータからデータが取り込まれることを意味します。入力フォースの変更例の値104を107に変更し、スクロールダウンして(EditTextが)ビューポートで長くなり、スクロールバックすると再び値104が返されます。 RecyclerViewは古い値をキャッシュしていて、そのデータバインディングライブラリはモデルを更新していないようです(はい、私はBindable属性と他の必要なものを使用しています)。また、アダプタ内でexecutePendingBindingsメソッドを使用します。recyclerview内のアンドロイドデータバインド

これは私のRecyclerViewアダプタである:これは私のモデルである

< ?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:bind="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools"> 
    <data> 
    <import type="com.adriagate.adriagateonlineandroid.models.Attribut"/> 
    <variable 
     name="attribute" 
     type="com.adriagate.adriagateonlineandroid.models.Attribut" /> 
    <import type="android.view.View"/> 
    <variable name="handlers"    
     type="com.adriagate.adriagateonlineandroid.MyHandlers"/> 

</data> 

<LinearLayout 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@{attribute.Question}"/> 

<EditText 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="@{attribute.Answer}" 
    android:inputType="number" 
    bind:visibility="@{attribute.isNumber ? View.VISIBLE : View.GONE}" 
    /> 

<EditText 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="@{attribute.Answer}" 
    android:inputType="text" 
    bind:visibility="@{attribute.isString ? View.VISIBLE : View.GONE}" 
    /> 
<android.support.v7.widget.AppCompatSpinner 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    bind:adriagateentries="@{attribute.Choices}" 
    bind:visibility="@{attribute.isSelect ? View.VISIBLE : View.GONE}" 
    bind:selectedId="@{attribute.ChoiceId}" 
    style="@style/Widget.AppCompat.Spinner.Underlined" 
    /> 
<CheckBox 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
bind:checked="@{attribute.Selected}" 
bind:visibility="@{attribute.isBool ? View.VISIBLE : View.GONE}"/> 


    </LinearLayout> 
    </layout> 

:あなたが有効にすることができますAndroidのメーカー2.1、と

public class Attribute extends BaseObservable { 

@Bindable 
public String getAnswer() { 
    return Answer; 
} 

public void setAnswer(String answer) { 
    Answer = answer; 
    notifyPropertyChanged(BR.answer); 
} 



public List<Choice> getChoices() { 
    return Choices; 
} 

public void setChoices(List<Choice> choices) { 
    Choices = choices; 
} 



public int getType() { 
    return Type; 
} 

public void setTypeype(int attributeType) { 
    Type = attributeType; 
} 

@Bindable 
public String getChoiceId() { 
    return ChoiceId; 
} 

public void setChoiceId(String choiceId) { 
    ChoiceId = choiceId; 
    notifyPropertyChanged(BR.choiceId); 
} 

@Bindable 
private String Answer; //tipovi 1 


@Bindable 
private String ChoiceId; //tipovi 1 

private List<Choice> Choices; //za tip 3 

private int Type; 

public boolean isNumber() { 
    return this.isNumber = this.getType()==1; 
} 
private boolean isNumber ; 

public boolean isSelect() { 
    return this.isSelect = this.getType()==3; 
} 
private boolean isSelect ; 

public boolean isString() { 
    return this.isString = this.getType()==2; 
} 
private boolean isString ; 

private boolean isBool ; 
public boolean isBool() { 
    return this.isBool = this.getType()==5; 
} 

private boolean isChecked; 
public boolean isChecked() { 
    return this.Answer =="D"; 
} 

} 
+0

エディットテキストに変更を取り、あなたのデータモデルにそれらを適用するコードですか? –

+0

ユーザーはappを使用し、EditText内で値を変更します。双方向データバインディングライブラリはモデルを更新します。それはそのように動作するはずです。 anglejsのようなWeb世界での2方向データ結合ライブラリは、どのように動作し、これはデータバインディングがどのように機能するかということです。 –

+0

1)編集テキストに変更を加えてモデルに残すコードは表示されません。私はあなたのためにそれを行うつもりの魔法の接着剤を記述するAndroidのデータバインディングのページに何も表示されません。 2)データモデルが変更されたときに通知機能を呼び出すことはありません。 –

答えて

1

public class SimpleAttributesAdapter extends RecyclerView.Adapter<SimpleAttributesAdapter.AttributesViewHolder> { 


private final Context context; 
private final List<Attribut> allAttributes; 

public SimpleAttributesAdapter(Context context,List<Attribut> _allAttributes) 
{ 
    this.context=context; 
    this.allAttributes=_allAttributes; 
} 


@Override 
public AttributesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    LayoutInflater inflater=LayoutInflater.from(context); 
    RecyclerViewAttributesChildOneRowBinding binding= DataBindingUtil.inflate(inflater, R.layout.recycler_view_attributes_child_one_row, parent, false); 
    MyHandlers handlers = new MyHandlers(); 
    binding.setHandlers(handlers); 
    return new AttributesViewHolder(binding); 
} 

@Override 
public void onBindViewHolder(AttributesViewHolder holder, int position) { 
    RecyclerViewAttributesChildOneRowBinding binding=holder.getViewDataBinding(); 
    binding.setAttribute(allAttributes.get(position)); 
    holder.getViewDataBinding().executePendingBindings(); 

} 

@Override 
public int getItemCount() { 
    return allAttributes.size(); 
} 

public class AttributesViewHolder extends RecyclerView.ViewHolder { 

     private RecyclerViewAttributesChildOneRowBinding binding; 

    public AttributesViewHolder(RecyclerViewAttributesChildOneRowBinding viewDataBinding) 
    { 
     super(viewDataBinding.getRoot()); 
     binding=viewDataBinding; 
     binding.executePendingBindings(); 
    } 
    public RecyclerViewAttributesChildOneRowBinding getViewDataBinding() { 
     return binding; 
    } 
} 
} 

これは私のレイアウトファイルでありますレイアウト内の "@ = {}"構文を使用して双方向データバインディングを作成します。

例えば、交換してください:

@{attribute.Answer} 

によって:

@={attribute.Answer} 

をあなたのレイアウトファイルに。複数の基準については

、このチュートリアルを確認することができますが:https://halfthought.wordpress.com/2016/03/23/2-way-data-binding-on-android/

関連する問題