美文网首页Android工具集
DataBind第二篇:RecyclerView Adapter

DataBind第二篇:RecyclerView Adapter

作者: Small_Cake | 来源:发表于2019-06-22 11:55 被阅读4次

    原文链接:https://www.jianshu.com/p/c219536a940a

    相信很多人用的适配器都是目前战力1万7的 BaseRecyclerViewAdapterHelper,没错,我也是用的这把战器。然后我们把这把战器镶嵌一颗DataBind宝石,让其战力更为强大。

    1.写一个DataBindBaseViewHolder
    public  class DataBindBaseViewHolder extends BaseViewHolder {
        private ViewDataBinding binding;
        public DataBindBaseViewHolder(@NonNull View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
        }
        public ViewDataBinding getBinding() {
            return binding;
        }
    }
    
    2.使用:
    public class DataBindDemoAdapter extends BaseQuickAdapter<MenuItem,DataBindBaseViewHolder> {
        public DataBindDemoAdapter() {
            super(R.layout.item_binding);
        }
        @Override
        protected void convert(DataBindBaseViewHolder helper, MenuItem item) {
            //这里的ItemBindingBinding就是item_binding去下划线后的骆驼命名+Binding
            ItemBindingBinding binding = (ItemBindingBinding) helper.getBinding();
            binding.setItem(item);
            binding.ivIcon.setImageResource(item.getImgRes());
        }
    }
    

    item_binding布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <layout>
        <data>
            <variable
                name="item"
                type="com.smallcake.model.MenuItem" />
        </data>
        <android.support.constraint.ConstraintLayout 
            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"
            android:layout_width="match_parent"
            android:layout_height="80dp">
            <ImageView
                android:id="@+id/iv_icon"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_marginTop="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:src="@mipmap/ic_launcher_round" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="@{item.title}"
                android:textSize="14sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/iv_icon"
                tools:text="SmallCaek" />
        </android.support.constraint.ConstraintLayout>
    </layout>
    

    特点:

    • 1.使用DataBind一键设置数据对象数据,不用在适配器中写一堆setText
    • 2.xml中不用写id属性,除非你要在适配器代码中使用

    相关文章

      网友评论

        本文标题:DataBind第二篇:RecyclerView Adapter

        本文链接:https://www.haomeiwen.com/subject/dmdmqctx.html