2

I have fixed number of items in recycler view, but I update data every 10 seconds and calling notifyDataSetChanged();

When I scrolled to Nth item (EX:20th item) and on notifyDataSetChanged() called recycler view getting refreshed and auto-scroll back to 1st item. How can I stop scrolling and update the recycler view? Thanks in advance.

  • I don't have any special index... I have a fixed number of items. Do I need to use the maximum index? – ChandraMouli Poreddy Dec 27 '19 at 12:32
  • Most of us do `adapter.notifyDataSetChanged()` once list object is update. You can also try by creating a method to in `RecyclerAdapter` class which will update the list object of adapter class and then just calls `notifyDataSetChanged()` within it. – Harpreet Dec 27 '19 at 12:59
  • provide some code, to understand how to help you. – Yurii Dec 15 '22 at 13:18

3 Answers3

0

You can create a view type and holder for your first item then set setIsRecyclable

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    if (holder instanceof HeaderViewHolder ) {
        ((HeaderViewHolder ) holder).setIsRecyclable(false);
        ((HeaderViewHolder ) holder).bind(mData.get(position));
    }
}
ysfcyln
  • 2,857
  • 6
  • 34
  • 61
  • What is HeaderViewHolder here? – ChandraMouli Poreddy Dec 27 '19 at 12:45
  • It is a normal `ViewHolder`. You should use two different `ViewHolder`. One is for header and the other is for normal items. Then disable `recycle` for your header type. You can check [this question](https://stackoverflow.com/questions/36313079/i-want-my-recyclerview-to-not-recycle-some-items) – ysfcyln Dec 27 '19 at 12:55
0

Using onSaveInstanceState you will save the state or current position of your recyclerview.Just like this.

Create variable in your activity

Parcelable recylerViewList; //this will store the position

then create this method to fetch recyclerview state

void storeInstance() {
    AttachmentsActivity.recylerViewList=mRecyclerView?.layoutManager?.onSaveInstanceState()
}// call this method before notifyDataSetChanged()

Create method to restore the state

    void restore() //call this method after notifyDataSetChanged()
    {
     mRecyclerView?.layoutManager?.onRestoreInstanceState(recylerViewList)
    }

Make sure you call this methods in activity or through activity object.

Vajani Kishan
  • 293
  • 4
  • 13
Rahul sharma
  • 1,492
  • 12
  • 26
0

You should find which items changed and use adapter.notifyItemChanged(itemChangedPosition) instead of notifyDatasetChanged()

Syscall
  • 19,327
  • 10
  • 37
  • 52
Na Pro
  • 715
  • 1
  • 8
  • 23