I have a ParentData
class structured as follows:
class ParentData(
//parent data fields
var childList: List<ChildData>
I am populating a parent RecylerView with a list of ParentData
and then populating each nested child RecyclerView with the embedded List<ChildData>
in onBindViewHolder
of the parent RecylerView Adapter (ListAdapter) like so:
val mAdapter = ChildAdapter()
binding.childRecyclerView.apply {
layoutManager = LinearLayoutManager(this.context)
adapter = mAdapter
}
mAdapter.submitList(item.childList)
//item from getItem(position)
I observe a LiveData<List<ParentData>>
, so every time the embedded ChildData
changes, I submit the new List<ParentData>
to my parent recycler, which in turn calls OnBindViewHolder
and submits the new `childList' and updates the inner child RecyclerView.
The issue is val mAdapter = ChildAdapter()
is called every time the data is updated, resetting the entire child RecyclerView. This results in no item add animations being seen and scroll position being reset.
Is there any way to avoid initialising a new Adapter every time or some other way I can avoid resetting the child RecyclerViews?