0

我花了很长时间试图找到这个问题的原因。我有一个具有几种不同视图类型的 RecyclerView,但我遇到的问题是我的 1 个视图持有者 ( ViewHolderA) 中的点击事件。由于我收到这些错误,我不知道我还应该使用什么。我认为我的项目类别可能是问题,但我不确定我哪里出错了。

'getter for position: Int' 在 Java 中已被弃用

活动课

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 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类

data class ItemA(val txtA1: String,
                 val txtA2: String,
                 var isExpanded: Boolean = false)

B类

data class ItemB(val txtB1: String)

C类

data class ItemC(val txtC1: String,
                 val txtC2: String,
                 val txtC3: String,
                 val txtC4: String,
                 var isExpanded: Boolean = false)

D类

data class ItemD(val txtD1: String,
                 val txtD2: String,
                 val txtD3: String,
                 val txtD4: String)

在此处输入图像描述

更新

在此处输入图像描述

4

1 回答 1

0

我认为问题在于适配器中的这一行 val isExpandable : Boolean = mData[position].isExpanded 您基本上是在使用适配器的getPosition()功能。如果您必须在适配器中安装单击侦听器,请将其放入onBindViewHolder,这样您的position参数会更准确,因为您有不止一种类型的列表项

于 2020-09-18T01:18:03.250 回答