Focus lost on refresh in recyclerview

前端 未结 2 1715
失恋的感觉
失恋的感觉 2021-02-09 05:33

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.notifyDat

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-09 06:01

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

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

    1. Override method getItemId(), give a stable id to each item:
      @Override public long getItemId(int position) { return position; }
    2. Set has StableId:
      adapter.setHasStableIds(true);
    3. Now basically you can keep focus in a concrete item when call notifyDatasetChange, if not, disable animator:
      mRecyclerView.setItemAnimator(null);

    about stable id:Android: How to make an adapter with stable ids?
    about disable animator:How to implement ItemAnimator of RecyclerView to disable the animation of notifyItemChanged
    about the reason of step3 in google code:
    https://code.google.com/p/android/issues/detail?id=204277

    Good luck!

提交回复
热议问题