随着app界面日益完善,普通的布局构建方式已经无法满足开发者追求高效编写代码的需求,于是 BaseBinderAdapter 诞生了,它继承于BaseQuickAdapter。
先看一下效果:
接下来就讲解一下 使用步骤,使用Kotlin+MVC来进行演示。(注:这里使用 MVVM使用效果更佳)
BaseBinderAdapter是一个三方的适配器,所以导入依赖之后就可以在相应的模块下直接引用了,
具体使用方法如下:
-
首先要创建需要用到的布局样式,这里只写一种其他样式写法大体相同,
首先 编写需要的条目布局样式-即XML文件,然后通过 DataBind 进行绑定
在该XML布局样式中需要用到哪些参数即编写相对应的实体类,通过DataBind进行数据绑定 -
编写布局样式条目,该类需要继承 QuickDataBindingItemBinder 它有两个泛型参数,第一个是实体类,第二个是布局Bind. 给该类中的实体类创建一个扩展函数即-Unit,以便在使用中通过实体类中的ID或者其他参数进行相应的事件处理
-
在VM层中创建一个泛型为 Any 的集合,用来存储不同的条目布局数据,可以通过LiveData 进行数据相应传输,切记在 VM 创建的时候即-onCreate() 方法中进行数据的添加以及数据相应。
-
在 V 层,进行正常的数据操作即可即-RecyclerView 添加条目布局等操作。最后在 LiveData 相应事件中为 Adapter 添加数据
具体代码实现如下:
- 布局XML:
activity_main:
<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>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</layout>
条目布局:
item_recycler_view:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="entity"
type="com.example.basebinderadapter_test.RecyclerViewEntity" />
</data>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_content"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@android:color/holo_blue_bright" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{entity.textContent}"
android:textStyle="bold"
android:textColor="@android:color/black"
android:textSize="16dp" />
</LinearLayout>
</layout>
实体类:
RecyclerViewEntity:
class RecyclerViewEntity(
var id:String = "",
var imagePath:Int = 0,
var textContent:String = ""
):Serializable
条目类:
class RecyclerViewChoose(
var itemClickBlock: (data: RecyclerViewEntity) -> Unit = {}
):
QuickDataBindingItemBinder<RecyclerViewEntity, ItemRecyclerViewBinding>() {
override fun convert(
holder: BinderDataBindingHolder<ItemRecyclerViewBinding>,
data: RecyclerViewEntity
) {
holder.dataBinding.apply {
entity = data
/**这里进行操作*/
if (data.imagePath != 0){
Glide.with(context).load(data.imagePath).into(holder.dataBinding.ivContent)
}
/**点击事件*/
holder.itemView.setOnClickListener {
itemClickBlock(data)
}
executePendingBindings()
}
}
override fun onCreateDataBinding(
layoutInflater: LayoutInflater,
parent: ViewGroup,
viewType: Int
): ItemRecyclerViewBinding = ItemRecyclerViewBinding.inflate(layoutInflater, parent, false)
}
Activity:
MainActivity:
class MainActivity : AppCompatActivity() {
var mBinding:ActivityMainBinding? = null
var mAdapter = BaseBinderAdapter()
val listData = ArrayList<Any>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_main)
/**布局于DataBind绑定*/
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
/**数据相关*/
initRecord()
/**控件相关*/
initUI()
}
private fun initRecord(){
/**设置每个条目数据*/
val item1 = RecyclerViewEntity("1",R.mipmap.ic_launcher,"苹果")
val item2 = RecyclerViewEntity("2",R.mipmap.ic_launcher,"香蕉")
val item3 = RecyclerViewEntity("3",R.mipmap.ic_launcher,"橘子")
val item4 = RecyclerViewEntity("4",R.mipmap.ic_launcher,"西瓜")
val item5 = RecyclerViewEntity("5",R.mipmap.ic_launcher,"柠檬")
val item6 = RecyclerViewEntity("6",R.mipmap.ic_launcher,"葡萄")
val item7 = RecyclerViewEntity("7",R.mipmap.ic_launcher,"柚子")
/**为集合添加数据*/
listData.add(item1)
listData.add(item2)
listData.add(item3)
listData.add(item4)
listData.add(item5)
listData.add(item6)
listData.add(item7)
}
private fun initUI(){
/**获取条目布局,并通过addItem为适配器添加布局*/
var itemView = RecyclerViewChoose()
mAdapter.apply {
addItemBinder(RecyclerViewEntity::class.java,itemView)
}
/**RecyclerView相关*/
mBinding!!.rvContent.apply {
layoutManager = GridLayoutManager(
context,
4,
GridLayoutManager.VERTICAL,
false
).apply{
spanSizeLookup = object : GridLayoutManager.SpanSizeLookup(){
override fun getSpanSize(position: Int): Int {
/**此处根据不同的参数来区分占空数*/
return if(
position < 4
){
1
}else if (position in 4..5){
2
}else {
4
}
}
}
}
adapter = mAdapter
}
/**为适配器添加数据*/
mAdapter.setList(listData)
/**点击事件*/
itemView.itemClickBlock = {
clickItemManager(it.id)
}
}
private fun clickItemManager(
clickId: String
){
when(clickId){
"1"-> Toast.makeText(this,"苹果",Toast.LENGTH_SHORT).show()
"2"-> Toast.makeText(this,"香蕉",Toast.LENGTH_SHORT).show()
"3"-> Toast.makeText(this,"橘子",Toast.LENGTH_SHORT).show()
"4"-> Toast.makeText(this,"西瓜",Toast.LENGTH_SHORT).show()
"5"-> Toast.makeText(this,"葡萄",Toast.LENGTH_SHORT).show()
"6"-> Toast.makeText(this,"柠檬",Toast.LENGTH_SHORT).show()
"7"-> Toast.makeText(this,"柚子",Toast.LENGTH_SHORT).show()
}
}
}
这里把以上需要的依赖也列出来:
Module中
//RecyclerView
implementation 'androidx.recyclerview:recyclerview:1.1.0'
//RV适配器
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4'
//Glide
implementation 'com.github.bumptech.glide:glide:4.11.0'
Project中:
maven { url "https://jitpack.io" }
至此;BaseBinderAdapter 就学会了