drSefa
drSefa

Reputation: 11

position doesn't work depends on getItemCount in RecyclerView (Kotlin)

override fun getItemCount(): Int { return ekipListesi.size }

override fun onBindViewHolder(holder: KisilerVH, position: Int) { holder.binding.kisiAdiTextView.text = ekipListesi[position] println(itemCount) println(position) }

ItemCount return respectively 5 and 6 as I hope, but it creates one card for each called not 5 or 6. So that position doesn't change 0,1,2,3,4 and 0,1,2,3,4,5

Position return just 0 in two case 1 times. I don't understand why this problem occured

I want to use create nested recyclerview. Parent works properly. However, "position" in childRV is doesn't work.

Upvotes: 1

Views: 42

Answers (1)

class ParentAdapter(private val parentList: List<ParentItem>) : RecyclerView.Adapter<ParentAdapter.ParentViewHolder>() {
    // ...

    override fun getItemCount(): Int {
        return parentList.size
    }

    override fun onBindViewHolder(holder: ParentViewHolder, position: Int) {
        val parentItem = parentList[position]
        
        // parent item views

        // child RecyclerView
        val childAdapter = ChildAdapter(parentItem.childItemList)
        holder.childRecyclerView.adapter = childAdapter
    }
}

class ChildAdapter(private val childList: List<ChildItem>) : RecyclerView.Adapter<ChildAdapter.ChildViewHolder>() {
    // ...

    override fun getItemCount(): Int {
        return childList.size
    }

    override fun onBindViewHolder(holder: ChildViewHolder, position: Int) {
        val childItem = childList[position]
        
        // child item views
    }
}

RecyclerView

RecyclerView.Adapter

Upvotes: 0

Related Questions