RecyclerView适配器notifyDataSetChanged不起作用
问题描述:
我扩展了
RecyclerView.Adapter<RecyclerView.ViewHolder>
当我打电话时:
mRecyclerView.getAdapter().notifyDataSetChanged();
什么都没发生.
刷新视图的唯一方法是再次设置适配器(查看此答案):
The only way to refresh the view is to set again the adapter (see this answer):
mRecyclerView.setAdapter(new MyAdapter(...));
此解决方案有两个问题:
I have two issues with this solution:
- 当我再次设置适配器时,我可以在屏幕上看到闪烁的灯光
- 列表视图返回到第一位置.
有什么想法吗?
答
如果notifyDataSetChanged()
不会触发视图更新,则有可能您忘记了在RecyclerView上调用SetLayoutManager()
(就像我一样!).
只是不要忘记这样做:
Java代码:
If notifyDataSetChanged()
does not trigger view updates than there is a chance that you have forgotten to call SetLayoutManager()
on your RecyclerView (like I did!).
Just don't forget to do this:
Java code:
LinearLayoutManager layoutManager = new LinearLayoutManager(context ,LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager)
C#代码,我正在使用Xamarin.
var layoutManager = new LinearLayoutManager(Context, LinearLayoutManager.Vertical, false);
recyclerView.SetLayoutManager(layoutManager);
在致电recyclerView.SetAdapter(adapter)
之前;