简体   繁体   English

在 recyclerview kotlin 中加载更多内容时如何不重写项目?

[英]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.

相关问题 如何使用Retrofit在recyclerView中正确加载更多项目? (科特琳) - How to properly load more items in a recyclerView, using Retrofit ? (kotlin) 在列表视图中向下滚动时如何加载更多项目? - How can I load more items when scrolling down in listview? 当我加载更多数据时,如何防止recyclerview滚动开始 - How can i prevent recyclerview scrolling to start when i load more data 当我使用RecyclerView和Retrofit时如何加载更多内容? - How to load more when i using RecyclerView and Retrofit? Android:如何在recyclerView中在后台加载更多数据 - Android: How can I load more data in the background in recyclerView 如何检测滚动视图何时到达底部,以便可以向其中加载更多项目? - How can I detect when a scrollview has reached the bottom so that I can load more items into it? 如何在RecyclerView中实现更多负载? 我清单上有500件物品 - How to implement load more in a RecyclerView? I am getting 500 items in list 如何在 RecyclerView 中选择多个项目并将它们添加到 ArrayList(KOTLIN)? - How can I select multiple items in RecyclerView and add them to an ArrayList(KOTLIN)? 如何将项目添加到回收站视图 Kotlin - How to add Items to a recyclerview Kotlin 如何在 Kotlin 中创建分段回收视图? - How can I create a sectioned recyclerview in Kotlin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM