BaseRecyclerViewAdapterHelper
一个非常简单灵活且强大的adapter
GitHub:
https://github.com/CymChad/BaseRecyclerViewAdapterHelper
配套文档:
https://github.com/CymChad/BaseRecyclerViewAdapterHelper/blob/master/readme/1-BaseQuickAdapter.md
1. 导入
根目录build添加jitpack
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
添加依赖
dependencies {
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4'
}
2. 使用
1. 编写adapter
/**
* <pre>
* author : i m yours
* e-mail : onlyyoulove3@gmail.com
* time : 2020/08/23
* desc : 测试adapter
* version: 1.0
* </pre>
*/
class TestAdapter(var namelist: MutableList<String>) :
BaseQuickAdapter<String,BaseViewHolder>(R.layout.item_bluetooth,namelist) {
override fun convert(holder: BaseViewHolder, item: String) {
holder.setText(R.id.tv_bluetooth_name,item)
}
}
- R.layout.item_bluetooth:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="15dp">
<View
android:id="@+id/view"
android:layout_width="0dp"
android:layout_height="50dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@drawable/circle_white"/>
<TextView
android:id="@+id/tv_bluetooth_name"
android:textColor="@color/color_blue_light"
android:text="bluetooth_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="@id/view"
app:layout_constraintTop_toTopOf="@id/view"
app:layout_constraintBottom_toBottomOf="@id/view"/>
<TextView
android:id="@+id/tv_bluetooth_mac"
android:textColor="@color/color_gray"
android:text="bluetooth_mac"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:textSize="18sp"
app:layout_constraintRight_toRightOf="@id/view"
app:layout_constraintTop_toTopOf="@id/view"
app:layout_constraintBottom_toBottomOf="@id/view"/>
</androidx.constraintlayout.widget.ConstraintLayout>
2. adapter中使用
var namelist: MutableList<String> = arrayOf("笔记本电脑","红米手机","mi8").toMutableList()
var adapter : TestAdapter= TestAdapter(namelist)
rv_bluetooth_list.layoutManager = LinearLayoutManager(this)
rv_bluetooth_list.adapter = adapter
3. 指定点击事件
//item中子项点击事件注册(不要再adapter中注册,具体版本有区别,2.x是在adapter的convert中注册)
adapter.addChildClickViewIds(R.id.tv_bluetooth_name,R.id.tv_bluetooth_mac)
//添加具体item里子项点击事件
adapter.setOnItemChildClickListener { adapter, view, position ->
Log.d("click","on item child click")
if (view.id == R.id.tv_bluetooth_name){
ToastUtil.shortShow("您点击了name: ${view.id} 这是第{$position}个")
}else{
ToastUtil.shortShow("您点击了mac: ${view.id} 这是第{$position}个")
}
}
//整个item的点击事件
adapter.setOnItemClickListener { adapter, view, position ->
ToastUtil.shortShow("this is onitem click : $adapter $view $position")
Log.d("click","on item click")
}
总结
这个库让我印象深刻的原因就是,点击事件实在是太好写了,而且如果是一个List组成的信息需要填充,是非常方便的,只不过每一个版本点击事件的写法都有些不同,具体细节详见上面的GitHub。
版权声明:本文为qq_43203586原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。