前言
basequickadapter是由BRVAH(官方网站)提供的万用适配器,相比原始的适配器,能减少70%的代码
空布局:adapter.setEmptyView(R.layout. layout_list_empty, binding.rvRevertBottom),第一个参数为空布局的layout,第二个参数为recycleview
一、basequickadapter结合recycleview的简单使用
效果图:
引入包:
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.28'
引入binding,在build.gradle里面加入
buildFeatures {
dataBinding = true
viewBinding true
}
setting里面的两个repositories地方都加入
maven { url "https://jitpack.io" }
activity的代码
class MainActivity : AppCompatActivity() {
private var _binding: ActivityMainBinding? = null
private val binding get() = _binding!!
private lateinit var adapter: ProjectRevertAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
initRecyclerView(getProjectDetailData())
}
private fun initRecyclerView(list: List<CProjectHeadBean>) {
adapter = ProjectRevertAdapter(R.layout.item_project_detail, list)
val mPerfectCourse = LinearLayoutManager(this)
mPerfectCourse.orientation = LinearLayoutManager.VERTICAL// 设置 recyclerview 布局方式为横向布局
binding.rvRevertBottom.layoutManager = mPerfectCourse
// adapter.setEmptyView(R.layout.layout_list_empty, binding.rvRevertBottom)
binding.rvRevertBottom.adapter = adapter
adapter.onItemChildClickListener =
BaseQuickAdapter.OnItemChildClickListener { adapter, view, position ->
try {
when (view.id) {
R.id.rcl_revert_cl_contain -> {
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
详细的代码可到这里下载:https://download.csdn.net/download/wy313622821/87507403
单选:
// 单选
fun singleChoice(pos: Int) {
if (selectPosition == pos) return
val item = data[selectPosition]
if (item is LeftShelvesBean) {
item.isSelect = false
notifyItemChanged(selectPosition)
}
val itemCurrent = data[pos]
if (itemCurrent is LeftShelvesBean) {
itemCurrent.isSelect = true
notifyItemChanged(pos)
}
selectPosition = pos
}
多选:
fun multipleChoice(position: Int) {
val item = data[position]
if (item is ConsumeBean) {
item.isSelect = !item.isSelect
notifyItemChanged(position)
}
}
二、二级子项
效果图:
需要代码的朋友可以到这里下载:https://download.csdn.net/download/wy313622821/87508264
三、二级子项(泛型版本)
产生的背景:当你想两个页面用不同的两个子项的时候,又不想写两个基本完全一样的CProjectHeadBean的时候(只是其中的AbstractExpandableItem<T>()不一样),那就需要用到泛型来代替
demo下载地址:https://download.csdn.net/download/wy313622821/87525537
四、网格(多行多列)
简单的使用,效果图:
demo下载地址:https://download.csdn.net/download/wy313622821/87542124
附加知识:
想在预览图上显示recyclerview的item的话,可以在recyclerview加
tools:itemCount="1"
tools:listitem="@layout/item_label_setting_info"