RecyclerView适配器notifyDataSetChanged不起作用 [英] RecyclerView Adapter notifyDataSetChanged not working

查看:79
本文介绍了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:

  1. 当我再次设置适配器时,我可以在屏幕上看到闪烁的灯光
  2. 列表视图返回到第一位置.

有什么想法吗?

推荐答案

如果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)之前;

这篇关于RecyclerView适配器notifyDataSetChanged不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆