I'm using navigation component in my app and for get data from api i'm using retrofit in MVVM architecture, i want get data from api and display in nested RecyclerView, this is worked and not problem for display data into nested Recylerview but when go to fragment detail and back to previous fragment not saved state and position item in horizontal list ,how to can display current position RecyclerView when back to previous fragment?
parent adapter
import kotlinx.android.synthetic.main.item_main_shaping_group.view.*
class MainShapingAdapter(
private val listGroup: MutableList<MainModel>,
private val listener: ListItemClick
) : RecyclerView.Adapter<MainShapingAdapter.MyViewHolder>(),
MainShapingChildAdapter.ListItemClickChild {
private val viewPool = RecyclerView.RecycledViewPool()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val layout = LayoutInflater.from(parent.context)
.inflate(R.layout.item_main_shaping_group, parent, false)
return MyViewHolder(layout)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.itemView.apply {
tv_titleGroup_itemGroup.text = listGroup[position].category.categoryTitle
rv_itemGroup.layoutManager =
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true)
rv_itemGroup.adapter = MainShapingChildAdapter(
listGroup[position].listProduct.toMutableList(),
this@MainShapingAdapter
)
rv_itemGroup.isNestedScrollingEnabled = false
rv_itemGroup.setRecycledViewPool(viewPool)
btn_more_itemGroup.setOnClickListener {
listener.itemOnclickCategory(listGroup[position].category)
}
}
}
override fun getItemCount(): Int = listGroup.size
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}
interface ListItemClick {
fun itemOnclickCategory(category: CategoryModel)
fun itemOnclickChild(product: Product)
}
override fun childOnclick(product: Product) {
listener.itemOnclickChild(product)
}
override fun onViewRecycled(holder: MyViewHolder) {
super.onViewRecycled(holder)
Log.d(ConstantApp.TAG, "onViewRecycled 1")
}
}
childe adapter
import kotlinx.android.synthetic.main.item_main_shaping_child.view.*
class MainShapingChildAdapter(
private val listProduct: MutableList<Product>,
private val listener: ListItemClickChild
) : RecyclerView.Adapter<MainShapingChildAdapter.MyViewHolder>() {
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val layout = LayoutInflater.from(parent.context)
.inflate(R.layout.item_main_shaping_child, parent, false)
return MyViewHolder(layout)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.itemView.apply {
Glide.with(context).load(listProduct[position].productCover)
.into(iv_coverProduct_shapingChild)
tv_titleProduct_shapingChild.text = listProduct[position].productTitle
tv_priceProduct_shapingChild.text = listProduct[position].productPrice.toString()
setOnClickListener {
listener.childOnclick(listProduct[position])
}
}
}
override fun getItemCount(): Int = listProduct.size
interface ListItemClickChild {
fun childOnclick(product: Product)
}
}