基于dataBinding、RecyclerView.Adapter封装的BaseAdapter

BaseAdapter

GitHub主页

  • 基于dataBinding、RecyclerView.Adapter实现的BaseAdapter。

  • 免去每次写adapter都需要使用DataBindingUtil.inflate()然后注入。

  • 并且footView的也不用单独处理,只需要传入布局ID即可和关联数据即可。

  • footView也兼容了单列(LinearLayoutManager)多列(GridLayoutManager)的样式

示例

0. 如何引入

0.1 根目录下的build.gradle添加如下代码
allprojects {
    repositories {
        google()
        jcenter()
        //添加下面这个maven配置
        maven{
            //这是maven仓库地址
            url 'https://raw.githubusercontent.com/xintanggithub/maven/master'
        }
    }
}
0.2 需要使用的module下build.gradle添加引用
    implementation "com.tson.utils.view:lib:1.0.5"

1. BaseAdapter的使用

1.1 新建item布局和foot布局
1.1.1 item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="vm"
            type="String" />

    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:background="@color/colorAccent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{vm}"
            android:textColor="#000000"
            tools:ignore="MissingConstraints"
            tools:layout_editor_absoluteX="97dp"
            tools:layout_editor_absoluteY="51dp" />
    </android.support.constraint.ConstraintLayout>
</layout>
1.1.2 item_faw_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="vm"
            type="String" />

    </data>

    <LinearLayout
        android:id="@+id/lv_root"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#336699"
        android:gravity="center"
        android:orientation="horizontal">

        <ProgressBar
            android:id="@+id/progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

        <TextView
            android:id="@+id/tv_tips"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            tools:text="没有更多" />

    </LinearLayout>
</layout>
1.2 创建adapter继承baseAdapter,并且设置footerView
1.2.1 创建adapter
// string为item对应的数据类类型、ItemLayoutBinding为item的dataBinding对象
public class MyAdapter extends BaseAdapter<String, ItemLayoutBinding> {

    MyAdapter(List<String> mData, int itemLayoutId, CallBack callBack) {
        super(mData, itemLayoutId, callBack);
    }

    @Override
    public void onBindViewHolders(int position, @NonNull BaseViewHolder holder) {
        ItemLayoutBinding itemLayoutBinding = (ItemLayoutBinding) holder.itemDataBinding;
        itemLayoutBinding.setVm(getData(position));
    }

}
1.2.2 配置footerView
  • 以下为配置footerView的代码,如果会多次使用并样式一样,建议统一封装回调方法

  • footerView的样式、不同状态的显示,baseAdapter均未做处理,给予自定义样式充分的空间

val data1 = mutableListOf("a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b","c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c","a", "b", "c")

    //依次传入 数据、item布局、设置footer的回调
    val layoutManager = GridLayoutManager(this@TestRecyclerViewActivity, 3)

    val adapter = MyAdapter(data, R.layout.item_layout, object : CallBack<String, ItemFawFooterBinding> {
    override fun layoutManager(): RecyclerView.LayoutManager {
                return layoutManager
            }
            
    override fun footerHolder(holder: BaseAdapter.FooterViewHolder<*>, mData: List<String>, loadState: Int) {
    //footerView显示逻辑的处理回调
        val item = holder.itemDataBinding as ItemFawFooterBinding
        if (mData.isEmpty()) {
            item.progress.visibility = View.INVISIBLE
            item.tvTips.text = ""
        } else {
            when (loadState) {//footerView的状态
                LOADING -> {//加载更多ing
                    item.progress.visibility = View.VISIBLE
                    item.tvTips.setText(R.string.loading_content_hint)
                }
                LOADING_COMPLETE -> {//加载完成
                    item.progress.visibility = View.INVISIBLE
                    item.tvTips.text = ""
                }
                LOADING_END -> {//没有更多
                    item.progress.visibility = View.GONE
                    item.tvTips.setText(R.string.no_more)
                }
                else -> {
                }
            }
        }
    }

    override fun dataBinding(parent: ViewGroup): ItemFawFooterBinding {
    //传入footerView的dataBinding对象
        return DataBindingUtil
                .inflate(LayoutInflater.from(parent.context),
                        R.layout.item_faw_footer, parent, false)
    }
})
1.2.3 绑定到recycleView
rv_list.layoutManager = LinearLayoutManager(this@TestRecyclerViewActivity)
rv_list.adapter = adapter
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值