在recyclelerView中进行数据绑定

时间:2018-02-23 04:32:54

标签: android realm android-databinding

我想在我的简单项目中使用DataBinding。根据android wiki进入我的Recyclerview适配器布局( rv_color_item.xml )项目我写了这段代码:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="shahramColor"
            type="com.groot.rang.model.ShahramColor"/>
    </data>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:context="com.groot.rang.PagerColorActivity">

        <android.support.v7.widget.CardView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:clickable="true"
            android:foreground="?android:attr/selectableItemBackground"
            app:cardCornerRadius="18dp"
            app:cardElevation="8dp">

            <ImageView
                android:id="@+id/imvItemColor"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:background="@{shahramColor.color}" />
        </android.support.v7.widget.CardView>
    </RelativeLayout>
</layout>

现在在onBindViewHolder方法的适配器中我写下这些代码:

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ShahramColor color = colors.get(position);
if (holder instanceof ColorViewHolder) {
    ((ColorViewHolder) holder).binding.setShahramColor(color);

但是当我想在模拟器中运行项目时,我收到了这个错误:

 Error:(10, 34) error: cannot find symbol class RvColorItemBinding

当我删除

<data>
    <variable
        name="shahramColor"
        type="com.groot.rang.model.ShahramColor"/>
</data>

android:background="@{shahramColor.color}"

我可以在模拟器中运行应用程序并且不会出现错误,一切正常。为什么我出错了?

这是我的pojo课程:

public class ShahramColor extends RealmObject implements Serializable{

    private String refrenceColor;
    private String color;
    ... setter and getter

*********************编辑******************

我已将此添加到gradle:

apply plugin: 'com.android.application'
apply plugin: 'realm-android'

android {
    dataBinding {
        enabled true
    }
    compileSdkVersion 26
    defaultConfig {...

1 个答案:

答案 0 :(得分:2)

适配器类:

public class ColorsRecyclerViewAdapter extends RecyclerView.Adapter<ColorsRecyclerViewAdapter.MyViewHolder> {
    private List<ShahramColor> data;


    public class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        private final ViewDataBinding binding;

        public MyViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void bind(Object obj) {
            binding.setVariable(BR.obj, obj);
            binding.executePendingBindings();
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public ColorsRecyclerViewAdapter(List<ShahramColor> myDataset) {
        data = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.rv_color_item, parent, false);
        // set the view's size, margins, paddings and layout parameters
        return new MyViewHolder(binding);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final ShahramColor colors = data.get(position);
        holder.bind(colors);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return data.size();
    }

}

模特课:

import android.databinding.BaseObservable;
import android.databinding.Bindable;

public class TemperatureData extends BaseObservable {

    private String location;

    private String celsius;

    private String url;

    public TemperatureData(String location, String celsius, String url) {
        this.location = location;
        this.celsius = celsius;
        this.url = url;
    }

    @Bindable
    public String getCelsius() {
        return celsius;
    }

    @Bindable
    public String getLocation() {
        return location;
    }

    @Bindable
    public String getUrl() {
        return url;
    }


    public void setLocation(String location) {
        this.location = location;
        notifyPropertyChanged(BR.location);
    }

    public void setCelsius(String celsius) {
        this.celsius = celsius;
        notifyPropertyChanged(BR.celsius);
    }

    public void setUrl(String url) {
        this.url = url;
        notifyPropertyChanged(BR.url);
    }
}