简单记录一下RecyclerView的单选实现
直接上代码 适配器是BRVAH 3.x版本
class MyAdapter(data: MutableList<String>) :
BaseQuickAdapter<String, BaseViewHolder>(
R.layout.item_view,
data
) {
var mPosition = -1
//Activity/Fragment Adapter.setOnItemClickListener 中调用次方法
fun setSelectedPosition(position: Int) {
mPosition = position
notifyDataSetChanged()
}
override fun convert(
holder: BaseViewHolder,
item: String
) {
holder.setText(R.id.tv_title, item)
if (holder.layoutPosition == mPosition) {
holder.itemView.setBackgroundResource(R.drawable.shape_style)
} else {
holder.itemView.setBackgroundColor(Color.TRANSPARENT)
}
}
}
结束!!!