在recyclelerview里面的android数据绑定

时间:2016-04-11 20:49:58

标签: android data-binding

我在recyclerview中有复选框,edittexts和其他输入。我成功实现了数据绑定,这意味着数据是从我从Web服务获得的数据中填充的。当我将一些输入foe示例更改值104更改为107并向下滚动(以便EditText)现在在视口中更长并向后滚动时我再次获得值104。似乎RecyclerView正在缓存旧值,我的数据绑定库没有更新模型。(是的我使用Bindable属性和其他必要的东西),我也在适配器中使用executePendingBindings方法。

这是我的RecyclerView适配器:

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;
    }
}
}

这是我的布局文件:

 < ?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>

这是我的模特:

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";
}

}

1 个答案:

答案 0 :(得分:1)

使用Android Studio 2.1,您可以使用布局中的“@ = {}”语法启用双向数据绑定。

例如,尝试替换:

@{attribute.Answer}

by:

@={attribute.Answer}

在你的布局文件中。

有关详细信息,请参阅本教程:https://halfthought.wordpress.com/2016/03/23/2-way-data-binding-on-android/