我在我的应用程序中使用了 fastadapter。从另一个屏幕返回时如何获取列表的保存状态。
我使用了 fastadapter 库中的现成方法,但由于某些原因它不起作用。
class ViewHolder(itemView: View) : FastAdapter.ViewHolder<ProgramsGalleryItem>(itemView) {
val rootVg = itemView.findViewById<ViewGroup>(R.id.root_vg)
val rv = itemView.findViewById<RecyclerView>(R.id.recycler_rv)
val titleTv = itemView.findViewById<TextView>(R.id.title_tv)
val layoutManager = LinearLayoutManager(rv.context, LinearLayoutManager.VERTICAL, false)
private val KEY_FASTADAPTER_STATE = "fastAdapter_state"
var mFastAdapterState = Bundle()
override fun bindView(item: ProgramsGalleryItem, payloads: MutableList<Any>) {
if (item.fastAdapter.itemCount == 0) {
rv.itemAnimator = null
if(item.isVertical) {
val lp = rv.layoutParams as LinearLayout.LayoutParams
lp.setMargins(lp.leftMargin, 0, lp.rightMargin, lp.bottomMargin)
rv.setPadding(0)
rv.layoutManager = layoutManager
rv.layoutParams = lp
}
val layoutId = if(item.isVertical){R.layout.item_common_program}else{R.layout.item_common_program_short}
val items = item.list.map { it.item(layoutId) }
val adapter = ItemAdapter<ProgramItem>()
adapter.add(items)
item.fastAdapter.addAdapter(0, adapter)
rv.adapter = item.fastAdapter
item.fastAdapter.withSavedInstanceState(mFastAdapterState, KEY_FASTADAPTER_STATE)
titleTv.text = item.title
}
}
override fun unbindView(item: ProgramsGalleryItem) {
rv.adapter = null
titleTv.text = null
item.fastAdapter.saveInstanceState(mFastAdapterState!!, KEY_FASTADAPTER_STATE)
item.fastAdapter = BindableFastAdapter()
}
}
}