如何使用片段内的实时数据更新 Recycler 视图?
How to update a Recycler view with live data inside a fragment?
我想更新我的 Recycler 视图,以便每当我从数据库中的 table 中删除一行时,它会立即更新 Recycler 视图而不刷新片段。
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View?
{
var binding:FragmentDisplayBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_display,container,false)
var view:View = binding.root
var new_task : FloatingActionButton = view.findViewById(R.id.add_newTask)
var db = Room.databaseBuilder(context!!,tasksDb:: class.java,"mydb").allowMainThreadQueries().build()
viewManager = LinearLayoutManager(context)
recyclerView = view.findViewById(R.id.recyclerView)
db.task().getAll().observe(this, Observer {
this.myAdapter = myAdapter(it)
recyclerView.adapter = myAdapter
recyclerView.layoutManager = viewManager
})
我认为我不应该在 onCreateView() 方法中使用 .observe()。我应该在代码中实施哪些更改??
也许这会有所帮助。
要在回收器中刷新您的项目,您需要使用以下代码
我的适配器。 notifyDataSetChanged();
recyclerview.setAdapter(我的适配器);
在您的情况下,在删除回收站视图项目的逻辑之后立即编写此方法。
我试图在 Internet 上找到一些示例解决方案。应该有,但是没找到。我发现其中很多都太短或太详细。所以我会把你需要的东西放在这里。有一个涉及 RecyclerView
+ LiveData
的标准化模式,因此对所有 RecyclerView
+ LiveData
用法都遵循此模式。
片段:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
...
val recyclerView = view.findViewById(R.id.recyclerView)
val myAdapter = MyAdapter()
recyclerView.adapter = myAdapter
// recyclerView.layoutManager = LinearLayoutManager(context) // You need this only if you haven't set it in xml
db.task().getAll().observe(viewLifecycleOwner, Observer {
myAdapter.submitList(it)
})
}
此处进行了一些重要更改:
- 您必须
.observe()
在 onCreateView()
。不仅针对这种特定情况,而且通常情况下,如果您正确使用 LiveData
,则永远不需要在任何其他地方调用 .observe()
。
viewLifecycleOwner
是用于观察 LiveData
的正确 LifecycleOwner
。 (除非您创建一个自定义的。)
- 取决于用例,但通常每个
RecyclerView
实例化一个适配器,即使您的数据随时间变化也是如此。您应该交换数据,而不是整个适配器。
我的适配器:
MyAdapter
应该实现视图
将使用的 .submitList()
函数
class MyAdapter: RecyclerView.Adapter<ViewHolder>() {
val myData = mutableListOf<Data>()
fun submitList(newData: List<Data>) {
myData.clear()
myData.addAll(newData)
notifyDataSetChanged()
}
}
请注意 notifyDataSetChanged()
实际上是通知适配器更新视图的信号。
改进:
鉴于您的列表不是太大或更新不是太频繁,我认为上述解决方案足以解决您的问题。如果您想进一步提高性能 and/or 可读性,请探索以下内容:
我想更新我的 Recycler 视图,以便每当我从数据库中的 table 中删除一行时,它会立即更新 Recycler 视图而不刷新片段。
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View?
{
var binding:FragmentDisplayBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_display,container,false)
var view:View = binding.root
var new_task : FloatingActionButton = view.findViewById(R.id.add_newTask)
var db = Room.databaseBuilder(context!!,tasksDb:: class.java,"mydb").allowMainThreadQueries().build()
viewManager = LinearLayoutManager(context)
recyclerView = view.findViewById(R.id.recyclerView)
db.task().getAll().observe(this, Observer {
this.myAdapter = myAdapter(it)
recyclerView.adapter = myAdapter
recyclerView.layoutManager = viewManager
})
我认为我不应该在 onCreateView() 方法中使用 .observe()。我应该在代码中实施哪些更改??
也许这会有所帮助。
要在回收器中刷新您的项目,您需要使用以下代码
我的适配器。 notifyDataSetChanged(); recyclerview.setAdapter(我的适配器);
在您的情况下,在删除回收站视图项目的逻辑之后立即编写此方法。
我试图在 Internet 上找到一些示例解决方案。应该有,但是没找到。我发现其中很多都太短或太详细。所以我会把你需要的东西放在这里。有一个涉及 RecyclerView
+ LiveData
的标准化模式,因此对所有 RecyclerView
+ LiveData
用法都遵循此模式。
片段:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
...
val recyclerView = view.findViewById(R.id.recyclerView)
val myAdapter = MyAdapter()
recyclerView.adapter = myAdapter
// recyclerView.layoutManager = LinearLayoutManager(context) // You need this only if you haven't set it in xml
db.task().getAll().observe(viewLifecycleOwner, Observer {
myAdapter.submitList(it)
})
}
此处进行了一些重要更改:
- 您必须
.observe()
在onCreateView()
。不仅针对这种特定情况,而且通常情况下,如果您正确使用LiveData
,则永远不需要在任何其他地方调用.observe()
。 viewLifecycleOwner
是用于观察LiveData
的正确LifecycleOwner
。 (除非您创建一个自定义的。)- 取决于用例,但通常每个
RecyclerView
实例化一个适配器,即使您的数据随时间变化也是如此。您应该交换数据,而不是整个适配器。
我的适配器:
MyAdapter
应该实现视图
.submitList()
函数
class MyAdapter: RecyclerView.Adapter<ViewHolder>() {
val myData = mutableListOf<Data>()
fun submitList(newData: List<Data>) {
myData.clear()
myData.addAll(newData)
notifyDataSetChanged()
}
}
请注意 notifyDataSetChanged()
实际上是通知适配器更新视图的信号。
改进:
鉴于您的列表不是太大或更新不是太频繁,我认为上述解决方案足以解决您的问题。如果您想进一步提高性能 and/or 可读性,请探索以下内容: