专注于recyclerview中的刷新 [英] Focus lost on refresh in recyclerview

查看:239
本文介绍了专注于recyclerview中的刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我正在开发Android TV应用程序,所以我水平和垂直使用了recyclerview,并且我使用了一种通过使用adapter.notifyDataSetChanged()刷新垂直recyclerview适配器的方法,但是我遇到了一个问题.1.它的焦点消失了,按任意一个D-pad键都无法正常工作,只有右键才能正常工作2.我使用了adapter.notifyItemRangeChanged(0,categoryDataList.size()),但我还有一个问题,它的重点是布局的第一个元素,否则布局的最后一个.因此,请尽快帮助我.预先感谢.

guys I am developing android TV app so I used recyclerview horizontally and vertically and I used a method to refresh adapter of vertical recyclerview by using adapter.notifyDataSetChanged() but I am getting one problem. 1. It's focus is being gone and on press any D-pad key it is not working only right key is working 2. I used adapter.notifyItemRangeChanged(0, categoryDataList.size()) but I have still one problem that it's focus is going on first element of layout otherwise last of layout. So Please help me as soon as possible. Thanks in advance.

推荐答案

当然,项目会失去重点.因为通过调用方法 notify * Changed 刷新RecyclerView时,没有任何视图可以聚焦.

Of course item will lose focus. Because no view to get focus when you refresh RecyclerView by calling method notify*Changed.

当您调用 notifyDatasetChanged()时,有一种不完善的方法可以使RecyclerView保持焦点.

There is an imperfect way to keep focus in RecyclerView when you call notifyDatasetChanged().

  1. 重写方法getItemId(),为每个项目赋予一个稳定的ID:
    @Override public long getItemId(int position){返回位置;}
  2. 集合具有StableId:
    adapter.setHasStableIds(true);
  3. 现在,基本上,您可以在调用notifyDatasetChange时将重点放在具体项目上,如果没有,请禁用动画制作器:
    mRecyclerView.setItemAnimator(null);

关于稳定ID: Android:如何制作适配器具有稳定的ID?
关于禁用动画师:如何实现ItemAnimatorRecyclerView的动画以禁用notifyItemChanged的动画
关于Google代码中step3的原因:
https://code.google.com/p/android/issues/detail?id = 204277

祝你好运!

这篇关于专注于recyclerview中的刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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