[英]how can I No Rewrite items when load more in recyclerview kotlin?
Community.社区。
I face a problem that a level of logic finds me locked我面临一个问题,即某种程度的逻辑发现我被锁定了
When I scroll brings new data, but when you enter the adapter, the items are rewritten instead of joining the ones that are already there.当我滚动时会带来新数据,但是当您进入适配器时,这些项目会被重写,而不是加入已经存在的项目。
in my fragment:在我的片段中:
private fun loadMoreProductList(root: View) {
val viewModel = ViewModelProvider(this).get(HomeViewModel::class.java)
viewModel.setCurrentPage().observe(
viewLifecycleOwner,
{
if (it != null) {
postListAdapter.setListDataMore(it, type_data, this)
postListAdapter.notifyDataSetChanged()
notLoading = true
progressbarHome.visibility = View.GONE
}
})
viewModel.makeApiCallListPost(
root.context, _page.toString()
)
}
override fun getLoadMore() {
_page = _page + 1
type_data = 1
loadMoreProductList(View(context))
}
............ ............ ............ ………………………………
in the viewmodel在视图模型中
var recyclerListPostList: MutableLiveData<List<PostData>> = MutableLiveData()
fun getRecyclerListPostObserver(): MutableLiveData<List<PostData>> {
return recyclerListPostList
}
fun setCurrentPage(): MutableLiveData<List<PostData>> {
recyclerListPostList.postValue(null)
return recyclerListPostList
}
fun makeApiCallListPost(context: Context, _page: String) {
val retroInstance = RetroInstance.getRetroInstance(context).create(
RetroService::class.java
)
val call = retroInstance.getPostData(
_page
)
call.enqueue(object : Callback<List<PostData>> {
override fun onResponse(call: Call<List<PostData>>, response: Response<List<PostData>>) {
if (response.isSuccessful) {
val destination = response.body()
destination?.let {
recyclerListPostList.postValue(response.body()!!)
}
} else {
recyclerListPostList.postValue(null)
}
}
override fun onFailure(call: Call<List<PostData>>, t: Throwable) {
t.printStackTrace()
recyclerListPostList.postValue(null)
}
})
}
in the adaptador在适配器中
fun setListData(
data: List<PostData>,
type_data: Int,
fragmentCallback: FragmentCallBack
) {
this.items0 = data as ArrayList<PostData>
this.fragmentCallback = fragmentCallback
}
fun setListDataMore(
data: List<PostData>,
type_data: Int,
fragmentCallback: FragmentCallBack
) {
items0.addAll(data as ArrayList<PostData>)
this.fragmentCallback = fragmentCallback
}
................ ................ ................ ................ …………………………………………………………………………………………………………………………………………………… .....................
I can provide more code if needed如果需要,我可以提供更多代码
Thank you.谢谢你。
The rewrite or bind all item is because you call重写或绑定所有项目是因为您调用
postListAdapter.notifyDataSetChanged()
notifyDataSetChanged() will refresh all the ViewHolder notifyDataSetChanged() 将刷新所有 ViewHolder
The easiest solution is just change最简单的解决方案就是改变
notifyDataSetChanged()
to notifyItemRangeChanged(startPosition, lastPosition(usually adapter size))
notifyDataSetChanged()
到notifyItemRangeChanged(startPosition, lastPosition(usually adapter size))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.