DataSourceProxy第三方框架
Posted maii
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataSourceProxy第三方框架相关的知识,希望对你有一定的参考价值。
RecyclerView其实都已经用了一段时间了,但是说实话,我是真的不太熟悉,而且看到自己项目用的这个BRVAH框架后,就觉得很神奇,因为之前不是刚学了下动画基础嘛,然后就想到处去实践,后来大佬就说这个框架很好用的,于是我就开始研究起来了,但是归根前提,还是简单说下RecyclerView的基础使用吧,这样也可以让小白们也看得懂,先附上最简单的用法哈:
看了这个之后是不是觉得还是挺好实现,哦哦,还有代码!!附上啦:
MyRecyclerViewAdapte,而ViewHolder作为内部类也写在里面了
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.concatadapter.MyRecyclerViewAdapter.MyViewHolder
import java.util.*
/**
* 1、继承RecyclerView.Adapter
* 2、绑定ViewHolder
* 3、实现Adapter的相关方法
*/
class MyRecyclerViewAdapter(private val mContext: Context, recyclerView: RecyclerView) : RecyclerView.Adapter<MyViewHolder?>() {
private var onItemClickListener: OnItemClickListener? = null
private val mRv: RecyclerView
private var dataSource: MutableList<String>
private var addDataPosition = -1
fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
this.onItemClickListener = onItemClickListener
}
fun setDataSource(dataSource: MutableList<String>) {
this.dataSource = dataSource
notifyDataSetChanged()
}
/**
* ViewHolder 绑定数据
* @param myViewHolder
* @param position
*/
override fun onBindViewHolder(@NonNull myViewHolder: MyViewHolder, position: Int) {
myViewHolder.mIv.setImageResource(getIcon(position))
myViewHolder.mTv.text = dataSource[position]
/**
* 只在瀑布流布局中使用随机高度
*/
if (mRv.layoutManager?.javaClass=== StaggeredGridLayoutManager::class.java) {
val params =
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, randomHeight)
myViewHolder.mTv.layoutParams = params
} else {
val params = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
myViewHolder.mTv.layoutParams = params
}
// 改变ItemView背景颜色
if (addDataPosition == position) {
myViewHolder.mItemView.setBackgroundColor(Color.RED)
} else {
myViewHolder.mItemView.setBackgroundColor(Color.parseColor("#A4D3EE"))
}
myViewHolder.mItemView.setOnClickListener { // 调用接口的回调方法
if (onItemClickListener != null) {
onItemClickListener!!.onItemClick(position)
}
}
}
/**
* 返回数据数量
* @return
*/
override fun getItemCount(): Int {
return dataSource.size
}
private fun getIcon(position: Int): Int {
when (position % 5) {
0 ->