【问题标题】:how can I No Rewrite items when load more in recyclerview kotlin?在 recyclerview kotlin 中加载更多内容时如何不重写项目?
【发布时间】:2021-06-27 00:58:13
【问题描述】:

社区。​​p>

我面临一个问题,逻辑层面发现我被锁定

当我滚动带来新数据,但当你进入适配器时,项目被重写而不是加入已经存在的项目。

在我的片段中:

    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))
}

............ ………… …………

在视图模型中

    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)
        }
    })
}

在适配器中

    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
}

....... ...... ...... ......

如果需要,我可以提供更多代码

谢谢。

【问题讨论】:

    标签: android kotlin android-recyclerview retrofit


    【解决方案1】:

    重写或绑定所有项目是因为你调用

    postListAdapter.notifyDataSetChanged()
    

    notifyDataSetChanged() 会刷新所有的 ViewHolder

    最简单的解决办法就是改变

    notifyDataSetChanged()notifyItemRangeChanged(startPosition, lastPosition(usually adapter size))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 2019-08-30
      相关资源
      最近更新 更多