Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

android - Notifying an item changed inside a nested adapter

I have one ParentAdapter that has multiple view holders and each view holder has its own RecyclerView and an adapter.

I am facing a problem when I need to notify an item of a child adapter is changed. I passed the position of the changed child view holder to the parent and I can use notifyItemChanged(position) but the whole child adapter is recreated.

What I need is to be able to notify a child item and keep the state of the child recyclerview as it is.

Here's the code of the Parent Adapter:

class ParentAdapter(): RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private lateinit var context: Context
    
    private var items = listOf<ParentItem>()

    companion object {
        const val CHILD_ONE = 1
        const val CHILD_TWO = 2
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        context = parent.context
        
        val layoutInflater = LayoutInflater.from(context)

        return when(viewType) {
            CHILD_ONE -> {
                ChildOneViewHolder(layoutInflater.inflate(R.layout.child_one, parent, false))
            }
            CHILD_TWO -> {
                ChildTwoViewHolder(layoutInflater.inflate(R.layout.child_two, parent, false))
            }
            else -> {
                BlankItemViewHolder(layoutInflater.inflate(R.layout.fragment_blank, parent, false))
            }
        }
    }

    fun update(items: List<Section>) {
        this.items = items
        notifyDataSetChanged()
    }

    override fun getItemViewType(position: Int): Int {
        
        val item = items[position]
        
        return when(item.type) {
            1 -> CHILD_ONE
            2 -> CHILD_TWO
            else -> -1
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when(holder.itemViewType) {
            CHILD_ONE -> {
                val section = items[position]
                
                val childOneAdapter = ChildOneAdapter(this)

                if(!section.items.isNullOrEmpty()) {
                    holder.rvChildOne.apply {
                        layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
                        adapter = childOneAdapter
                        isNestedScrollingEnabled = true
                    }

                    childOneAdapter.update(section.items)
                }
            }

            CHILD_TWO -> {
                val section = items[position]

                val childTwoAdapter = ChildTwoAdapter(this)

                if(!section.items.isNullOrEmpty()) {
                    holder.rvChildTwo.apply {
                        layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
                        adapter = childTwoAdapter
                        isNestedScrollingEnabled = true
                    }

                    childTwoAdapter.update(section.items)
                }
            }
            else ->
                initBlankSection(holder as BlankItemViewHolder, position)
        }
    }

    private fun initBlankSection(holder: BlankItemViewHolder, position: Int) {

    }

    private class ChildOneViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val rvChildOne: RecyclerView = itemView.findViewById(R.id.rv_child_one)
    }

    private class ChildTwoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val rvChildTwo: RecyclerView = itemView.findViewById(R.id.rv_child_two)
    }

    private class BlankItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {}

    override fun getItemCount(): Int = items.size
}
question from:https://stackoverflow.com/questions/66048578/notifying-an-item-changed-inside-a-nested-adapter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...