【发布时间】: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