![](/img/trans.png)
[英]Kotlin RecyclerView in RecyclerView after update a view is going to child RecyclerView
[英]How to update recyclerview after view was created with kotlin
我很難理解關於更新主線程的事情。 所以基本上我的片段中有一個 ReciclerView ,它工作正常,但只有在第一次加載布局時。 我試圖實現SwipeRefreshLayout
以防手機未連接到互聯網,用戶會刷新頁面,但回收器不起作用!
電話:
pullToRefresh = root.findViewById<SwipeRefreshLayout>(R.id.push);
pullToRefresh.setOnRefreshListener {
presenter?.netWorkCall();
}
響應:
override fun showResponse(data: List<CategoryModel>) {
if(data.isEmpty()){
waringText.text = "N/A"
waringText.visibility = View.VISIBLE;
waring.visibility = View.VISIBLE;
}else{
pullToRefresh.isRefreshing = false
recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL ,false)
recyclerView.adapter = MainCategoryAdapter(data,findNavController());
}
}
我怎樣才能更新這個東西?
您應該只將適配器設置為 recyclerView 一次,下次您應該調用recyclerView.adapter?.notifyDataSetChanged()
。 但是我從上面的代碼中看到的是,您正在再次設置適配器,這可能是導致此問題的原因。 使用此代碼:
override fun showResponse(data: List<CategoryModel>) {
if(data.isEmpty()){
waringText.text = "N/A"
waringText.visibility = View.VISIBLE;
waring.visibility = View.VISIBLE;
}else{
pullToRefresh.isRefreshing = false
if (recyclerView.adapter == null){
recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL ,false)
recyclerView.adapter = MainCategoryAdapter(data,findNavController())
}else{
recyclerView.adapter?.notifyDataSetChanged()
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.