尝试实现 RecyclerView 点击监听器时位置代码不正确
Incorrect position code when trying to implement RecyclerView click listener
我花了很长时间试图找到这个问题的原因。我有一个具有几种不同视图类型的 RecyclerView,但我遇到的问题是我的视图持有者 1 (ViewHolderA
) 中的单击事件。由于出现这些错误,我不知道我还应该使用什么。我认为我的项目 class 可能是问题所在,但我不确定哪里出了问题。
'getter for position: Int' is deprecated in Java
Activity class
class MainActivity : AppCompatActivity() {
private var mList: RecyclerView? = null
private var mRecyclerAdapter: MyAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mList = findViewById(R.id.listRV)
val listEntities = arrayListOf<Any>()
listEntities.add(ItemA("A1", "A2"))
listEntities.add(ItemB("B1"))
listEntities.add(ItemC("C1", "C2", "C3", "C4"))
listEntities.add(ItemD("D1", "D2", "D3", "D4"))
val layoutManager = LinearLayoutManager(this)
mList!!.layoutManager = layoutManager
mRecyclerAdapter = MyAdapter(listEntities)
mList!!.adapter = mRecyclerAdapter
}
}
适配器class
class MyAdapter(var mData: List<Any>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val typeA: Int = 1
private val typeB: Int = 2
private val typeC: Int = 3
private val typeD: Int = 4
override fun getItemViewType(position: Int): Int {
return when {
mData[position] is ViewHolderA -> {
typeA
}
mData[position] is ViewHolderB -> {
typeB
}
mData[position] is ViewHolderC -> {
typeC
}
mData[position] is ViewHolderD -> {
typeD
}
else -> -1
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
var layout = 0
val viewHolder: RecyclerView.ViewHolder?
when (viewType) {
typeA -> {
layout = R.layout.rv_item_a
val viewA = LayoutInflater.from(parent.context).inflate(layout, parent, false)
viewHolder = ViewHolderA(viewA)
}
typeB -> {
layout = R.layout.rv_item_b
val viewB = LayoutInflater.from(parent.context).inflate(layout, parent, false)
viewHolder = ViewHolderB(viewB)
}
typeC -> {
layout = R.layout.rv_item_c
val viewC = LayoutInflater.from(parent.context).inflate(layout, parent, false)
viewHolder = ViewHolderC(viewC)
}
typeD -> {
layout = R.layout.rv_item_d
val viewD = LayoutInflater.from(parent.context).inflate(layout, parent, false)
viewHolder = ViewHolderD(viewD)
}
else -> viewHolder = null
}
return viewHolder!!
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder.itemViewType) {
typeA -> {
val detailsA = mData[position] as ItemA
(holder as ViewHolderA).showDetails(detailsA)
val mClickListener = View.OnClickListener {
detailsA.isExpanded = !detailsA.isExpanded
notifyItemChanged(position)
val isExpandable : Boolean = detailsA.isExpanded
holder.tvA2.visibility = if (isExpandable) View.VISIBLE else View.GONE
holder.ibA.setImageResource(if (isExpandable) R.drawable.expand_less else R.drawable.expand_more)
}
holder.headerEConstraintLayout.setOnClickListener(mClickListener)
holder.headerEImageButtonA.setOnClickListener(mClickListener)
}
typeB -> {
val detailsB = mData[position] as ItemB
(holder as ViewHolderB).showDetails(detailsB)
}
typeC -> {
val detailsC = mData[position] as ItemC
(holder as ViewHolderC).showDetails(detailsC)
}
typeD -> {
val detailsD = mData[position] as ItemD
(holder as ViewHolderD).showDetails(detailsD)
}
}
}
override fun getItemCount(): Int {
return mData.size
}
inner class ViewHolderA(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val itemAcl: ConstraintLayout = itemView.findViewById(R.id.clA)
private val itemAib: ImageButton = itemView.findViewById(R.id.ibA)
private val itemA1: TextView = itemView.findViewById(R.id.tvA1)
private val itemA2: TextView = itemView.findViewById(R.id.tvA2)
}
inner class ViewHolderB(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val itemB1: TextView = itemView.findViewById(R.id.tvB1)
fun showDetails(detailsB: ItemB) {
val txtB1 = detailsB.txtB1
itemB1.text = txtB1
}
}
inner class ViewHolderC(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val itemCib: ImageButton = itemView.findViewById(R.id.ibC)
private val itemC1: TextView = itemView.findViewById(R.id.tvC1)
private val itemC2: TextView = itemView.findViewById(R.id.tvC2)
private val itemC3: TextView = itemView.findViewById(R.id.tvC3)
private val itemC4: TextView = itemView.findViewById(R.id.tvC4)
fun showDetails(detailsC: ItemC) {
val txtC1 = detailsC.txtC1
val txtC2 = detailsC.txtC2
val txtC3 = detailsC.txtC3
val txtC4 = detailsC.txtC4
itemC1.text = txtC1
itemC2.text = txtC2
itemC3.text = txtC3
itemC4.text = txtC4
}
}
inner class ViewHolderD(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val itemD1: TextView = itemView.findViewById(R.id.tvD1)
private val itemD2: TextView = itemView.findViewById(R.id.tvD2)
private val itemD3: TextView = itemView.findViewById(R.id.tvD3)
private val itemD4: TextView = itemView.findViewById(R.id.tvD4)
fun showDetails(detailsD: ItemD) {
val txtD1 = detailsD.txtD1
val txtD2 = detailsD.txtD2
val txtD3 = detailsD.txtD3
val txtD4 = detailsD.txtD4
itemD1.text = txtD1
itemD2.text = txtD2
itemD3.text = txtD3
itemD4.text = txtD4
}
}
}
项目 A class
data class ItemA(val txtA1: String,
val txtA2: String,
var isExpanded: Boolean = false)
项目 B class
data class ItemB(val txtB1: String)
项目 C class
data class ItemC(val txtC1: String,
val txtC2: String,
val txtC3: String,
val txtC4: String,
var isExpanded: Boolean = false)
项目 D class
data class ItemD(val txtD1: String,
val txtD2: String,
val txtD3: String,
val txtD4: String)
更新
我认为问题出在适配器中的这一行
val isExpandable : Boolean = mData[position].isExpanded
您基本上是在使用适配器的 getPosition()
功能。如果你必须在适配器中有点击监听器,把它放在 onBindViewHolder
中,这样你就有了更准确的 position
参数,因为你有不止一种类型的列表项
我花了很长时间试图找到这个问题的原因。我有一个具有几种不同视图类型的 RecyclerView,但我遇到的问题是我的视图持有者 1 (ViewHolderA
) 中的单击事件。由于出现这些错误,我不知道我还应该使用什么。我认为我的项目 class 可能是问题所在,但我不确定哪里出了问题。
'getter for position: Int' is deprecated in Java
Activity class
class MainActivity : AppCompatActivity() {
private var mList: RecyclerView? = null
private var mRecyclerAdapter: MyAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mList = findViewById(R.id.listRV)
val listEntities = arrayListOf<Any>()
listEntities.add(ItemA("A1", "A2"))
listEntities.add(ItemB("B1"))
listEntities.add(ItemC("C1", "C2", "C3", "C4"))
listEntities.add(ItemD("D1", "D2", "D3", "D4"))
val layoutManager = LinearLayoutManager(this)
mList!!.layoutManager = layoutManager
mRecyclerAdapter = MyAdapter(listEntities)
mList!!.adapter = mRecyclerAdapter
}
}
适配器class
class MyAdapter(var mData: List<Any>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val typeA: Int = 1
private val typeB: Int = 2
private val typeC: Int = 3
private val typeD: Int = 4
override fun getItemViewType(position: Int): Int {
return when {
mData[position] is ViewHolderA -> {
typeA
}
mData[position] is ViewHolderB -> {
typeB
}
mData[position] is ViewHolderC -> {
typeC
}
mData[position] is ViewHolderD -> {
typeD
}
else -> -1
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
var layout = 0
val viewHolder: RecyclerView.ViewHolder?
when (viewType) {
typeA -> {
layout = R.layout.rv_item_a
val viewA = LayoutInflater.from(parent.context).inflate(layout, parent, false)
viewHolder = ViewHolderA(viewA)
}
typeB -> {
layout = R.layout.rv_item_b
val viewB = LayoutInflater.from(parent.context).inflate(layout, parent, false)
viewHolder = ViewHolderB(viewB)
}
typeC -> {
layout = R.layout.rv_item_c
val viewC = LayoutInflater.from(parent.context).inflate(layout, parent, false)
viewHolder = ViewHolderC(viewC)
}
typeD -> {
layout = R.layout.rv_item_d
val viewD = LayoutInflater.from(parent.context).inflate(layout, parent, false)
viewHolder = ViewHolderD(viewD)
}
else -> viewHolder = null
}
return viewHolder!!
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder.itemViewType) {
typeA -> {
val detailsA = mData[position] as ItemA
(holder as ViewHolderA).showDetails(detailsA)
val mClickListener = View.OnClickListener {
detailsA.isExpanded = !detailsA.isExpanded
notifyItemChanged(position)
val isExpandable : Boolean = detailsA.isExpanded
holder.tvA2.visibility = if (isExpandable) View.VISIBLE else View.GONE
holder.ibA.setImageResource(if (isExpandable) R.drawable.expand_less else R.drawable.expand_more)
}
holder.headerEConstraintLayout.setOnClickListener(mClickListener)
holder.headerEImageButtonA.setOnClickListener(mClickListener)
}
typeB -> {
val detailsB = mData[position] as ItemB
(holder as ViewHolderB).showDetails(detailsB)
}
typeC -> {
val detailsC = mData[position] as ItemC
(holder as ViewHolderC).showDetails(detailsC)
}
typeD -> {
val detailsD = mData[position] as ItemD
(holder as ViewHolderD).showDetails(detailsD)
}
}
}
override fun getItemCount(): Int {
return mData.size
}
inner class ViewHolderA(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val itemAcl: ConstraintLayout = itemView.findViewById(R.id.clA)
private val itemAib: ImageButton = itemView.findViewById(R.id.ibA)
private val itemA1: TextView = itemView.findViewById(R.id.tvA1)
private val itemA2: TextView = itemView.findViewById(R.id.tvA2)
}
inner class ViewHolderB(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val itemB1: TextView = itemView.findViewById(R.id.tvB1)
fun showDetails(detailsB: ItemB) {
val txtB1 = detailsB.txtB1
itemB1.text = txtB1
}
}
inner class ViewHolderC(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val itemCib: ImageButton = itemView.findViewById(R.id.ibC)
private val itemC1: TextView = itemView.findViewById(R.id.tvC1)
private val itemC2: TextView = itemView.findViewById(R.id.tvC2)
private val itemC3: TextView = itemView.findViewById(R.id.tvC3)
private val itemC4: TextView = itemView.findViewById(R.id.tvC4)
fun showDetails(detailsC: ItemC) {
val txtC1 = detailsC.txtC1
val txtC2 = detailsC.txtC2
val txtC3 = detailsC.txtC3
val txtC4 = detailsC.txtC4
itemC1.text = txtC1
itemC2.text = txtC2
itemC3.text = txtC3
itemC4.text = txtC4
}
}
inner class ViewHolderD(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val itemD1: TextView = itemView.findViewById(R.id.tvD1)
private val itemD2: TextView = itemView.findViewById(R.id.tvD2)
private val itemD3: TextView = itemView.findViewById(R.id.tvD3)
private val itemD4: TextView = itemView.findViewById(R.id.tvD4)
fun showDetails(detailsD: ItemD) {
val txtD1 = detailsD.txtD1
val txtD2 = detailsD.txtD2
val txtD3 = detailsD.txtD3
val txtD4 = detailsD.txtD4
itemD1.text = txtD1
itemD2.text = txtD2
itemD3.text = txtD3
itemD4.text = txtD4
}
}
}
项目 A class
data class ItemA(val txtA1: String,
val txtA2: String,
var isExpanded: Boolean = false)
项目 B class
data class ItemB(val txtB1: String)
项目 C class
data class ItemC(val txtC1: String,
val txtC2: String,
val txtC3: String,
val txtC4: String,
var isExpanded: Boolean = false)
项目 D class
data class ItemD(val txtD1: String,
val txtD2: String,
val txtD3: String,
val txtD4: String)
更新
我认为问题出在适配器中的这一行
val isExpandable : Boolean = mData[position].isExpanded
您基本上是在使用适配器的 getPosition()
功能。如果你必须在适配器中有点击监听器,把它放在 onBindViewHolder
中,这样你就有了更准确的 position
参数,因为你有不止一种类型的列表项