9

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.

ajay
  • 101
  • 1
  • 6
  • Can you post your code for reference – EKN Oct 28 '16 at 06:45
  • onCreate(Bundle savedInstanceState) { . . adapter = new DemoVerticalRecyclerAdapter(this, categoriesVideoList, categoryDataList, mainMenuDataList); verticalListView.setAdapter(adapter); . . } – ajay Oct 28 '16 at 06:52
  • private void refreshedData() { runnable = new Runnable() { @Override public void run() { Log.e(TAG, "Data refreshed starrt......."); // isRefreshedData = true; new MyAsycTas().execute(); handler.removeCallbacks(runnable); focusedElement(); // select(); handler.postDelayed(runnable, 9000); } }; // handler.postDelayed(runnable, // Integer.valueOf(MyStaticClass.refreshTime) * 60000); handler.postDelayed(runnable, 9000); } – ajay Oct 28 '16 at 06:52
  • @Override protected void onPostExecute(Void result) { adapter.notifyItemRangeChanged(0, categoryDataList.size()); super.onPostExecute(result); } – ajay Oct 28 '16 at 06:55

2 Answers2

24

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!

RainFool
  • 446
  • 3
  • 15
1

Use

notifyItemRangeInserted(position,size); 

if you are inserting items.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • I have horizontal & vertical recyclerview and getting response from server and we have no idea that item is adding or removing so I am using notifyItemRangeChanged but my focus is going on top – ajay Oct 28 '16 at 07:33