一个项目可能会用到多个RecyclerView,而使用RecyclerView必定离不开Adapter,如果不对Adapter进行封装那么一个项目下来要写多少个Adapter,而这些Adapter代码多数都是一样的。今天来说下RecyclerViewAdapter的封装和使用。
Adapter
abstract class MyRecyclerAdapter<T> constructor(
private val context: Context,
list: ArrayList<T>,
private val resID: Int
): RecyclerView.Adapter<BaseViewHolder>() {
var data = list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
val view = LayoutInflater.from(context).inflate(resID,parent,false)
return BaseViewHolder(view)
}
override fun getItemCount(): Int {
return data.size
}
//点击事件
interface OnItemClickListerner{
fun onItemClick(position: Int)
}
private var mOnItemClickListerner:OnItemClickListerner? = null
fun setOnItemClickListerner(onItemClickListerner:OnItemClickListerner?){
mOnItemClickListerner = onItemClickListerner
}
override fun onBindViewHolder(
holder: BaseViewHolder,
position: Int,
payloads: MutableList<Any>
) {
// if (payloads.isEmpty()){ // 使用 notifyItemChanged(i,ddd[i]) ,notifyItemChanged(position,payloads) ,使用 notifyDataSetChanged()时 payloads为空
val item = data[position]
onBindData(holder,position,item)
// }else{
// val item1 = payloads[0]
// onBindData(holder,position,item1 as T)
// }
holder.itemView.setOnClickListener {
mOnItemClickListerner!!.onItemClick(position)
}
}
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
//不使用
}
protected abstract fun onBindData(holder: BaseViewHolder, position: Int, item: T)
fun Refresh(ddd : ArrayList<T>){
data = ddd
if (Looper.myLooper() != Looper.getMainLooper()){
(context as Activity).runOnUiThread {
// for (i in ddd.indices){
// notifyItemChanged(i,ddd[i])
// }
notifyDataSetChanged()
}
}
else{
// for (i in ddd.indices){
// notifyItemChanged(i,ddd[i])
// }
notifyDataSetChanged()
}
}
}
open class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}
配置RecyclerView
rv_test = findViewById(R.id.rv_test)
//瀑布流
//1)通过LinearLayoutManager:我们可以实现横向、纵向的列表效果
//2)通过GridLayoutManager:我们可以实现像GridView一样的网格布局
//3)通过StaggeredGridLayoutManager:我们可以实现瀑布流的效果
val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
//layoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE
rv_test.layoutManager = layoutManager
//设置增加或删除条目的动画
rv_test.setItemAnimator( DefaultItemAnimator())
Adapter使用
val adapter = object : MyRecyclerAdapter<String>(this,list,R.layout.itemview){
override fun onBindData(holder: BaseViewHolder, position: Int, item: String) {
textView = holder.itemView.findViewById(R.id.tv_test)
imageView = holder.itemView.findViewById(R.id.iv_test)
textView.text = item
val params = holder.itemView.layoutParams
params.height = (if (position % 2 == 0) 2 else 3) * 100 + 60 //设置左右视图为不同的高度
holder.itemView.layoutParams = params//仅仅改变高度
}
}
rv_test.adapter = adapter
adapter.setOnItemClickListerner(object : MyRecyclerAdapter.OnItemClickListerner{
override fun onItemClick(position: Int) {
Log.e("test","点击了 " + position)
}
})
}
刷新数据
adapter.Refresh(list)
Adapter 刷新
fun Refresh(ddd : ArrayList<T>){
data = ddd
if (Looper.myLooper() != Looper.getMainLooper()){
(context as Activity).runOnUiThread {
// for (i in ddd.indices){
// notifyItemChanged(i,ddd[i])
// }
notifyDataSetChanged()
}
}
else{
// for (i in ddd.indices){
// notifyItemChanged(i,ddd[i])
// }
notifyDataSetChanged()
}
}
大功告成