Android Databinding 与 RecycleView mvvm的运用

databinding 和RecycleView 需要Observable Binding双向绑定

public class SalesNewInfo extends BaseObservable {
    private String title;
    private Boolean isBegging;


    public String getTitle() {
        return title;
    }

    @Bindable
    public void setTitle(String title) {
        this.title = title;
        notifyPropertyChanged(BR.title);
    }

    public Boolean getBegging() {
        return isBegging;
    }



    @Bindable
    public void setBegging(Boolean begging) {
        isBegging = begging;
        notifyPropertyChanged(BR.begging);
    }

其中set的地方用到了@Bindable 和notifyPropertyChanged( )以便实现双向绑定
item layout中布局需要添加

    <data>

        <variable
            name="item"
            type="com.ubtechinc.cruzr.easyhomeapp.model.SalesNewInfo" />
    </data>
然后根据数据的不同实现显示不同其中图片的显示方法代码如下
       <ImageView
            android:id="@+id/imageView_banner"
            android:layout_width="175dp"
            android:layout_height="47dp"
            android:background="@{item.begging?       @drawable/item_sales_banner:@drawable/item_beginng_sales_banner}"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

然后是主要的adapter编写
自定义BindingViewHolder,并且继承RecyclerView.Adapter

private List<SalesNewInfo> mSaleNewsList;
    private final LayoutInflater mLayoutInflater;
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        /**
         * @time 2018/5/16  15:17
         * @describe 单个点击监听回调
         */
        void onsalesNewsClick(SalesNewInfo salesNews);
    }

    public SalesNewsAdapter(Context context) {
        mLayoutInflater = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mSaleNewsList = new ArrayList<>();
    }

    @Override
    public BindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewDataBinding binding;
        binding = DataBindingUtil.inflate(mLayoutInflater,
                R.layout.item_sales_promotion, parent, false);
        return new BindingViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(BindingViewHolder holder, int position) {
        final SalesNewInfo salesNewInfo = mSaleNewsList.get(position);
        holder.getBinding().setVariable(BR.item, salesNewInfo);
        holder.getBinding().executePendingBindings();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onsalesNewsClick(salesNewInfo);
                }
            }
        });

    }

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

    public void addAll(List<SalesNewInfo> salesNews) {
        mSaleNewsList.addAll(salesNews);
    }

    public void add(SalesNewInfo salesNew) {
        mSaleNewsList.add(salesNew);
        notifyItemInserted(mSaleNewsList.size());
    }

    public void remove(int position) {
        mSaleNewsList.remove(position);
        notifyItemRemoved(position);
    }

    public void setListener(OnItemClickListener listener) {
        mListener = listener;
    }

BindingViewHolder代码如下

public class BindingViewHolder<T extends ViewDataBinding>
        extends RecyclerView.ViewHolder {


    private T mBinding;

    public BindingViewHolder(T binding) {
        super(binding.getRoot());
        mBinding = binding;
    }

    public T getBinding() {
        return mBinding;
    }
}

最后在activity中使用

mAdapter = new SalesNewsAdapter(this);
        GridLayoutManager mGridLayoutManager = new GridLayoutManager(this, 2);
        mBinding.recyclerView.setLayoutManager(mGridLayoutManager);
        mBinding.recyclerView.setAdapter(mAdapter);
        List<SalesNewInfo> mlist = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            SalesNewInfo info = new SalesNewInfo();
            info.setTitle("lala" + i);
            if (i < 4) {
                info.setBegging(true);
            } else {
                info.setBegging(false);
            }
            mlist.add(info);
        }
        mAdapter.addAll(mlist);

        mAdapter.setListener(new SalesNewsAdapter.OnItemClickListener() {
            @Override
            public void onsalesNewsClick(SalesNewInfo salesNews) {

                SalesDetailInfoActivity.start(SalesPromotionActivity.this);
                Toast.makeText(SalesPromotionActivity.this, salesNews.getTitle(), Toast.LENGTH_LONG).show();
            }
        });
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页