Madhav_nimavat
Madhav_nimavat

Reputation: 401

Recyclerview automatically scroll up when data inserted

I'm suffering with one problem that when I add data to array list and then add it into adapter after that when I set it to recycler view adapter it jumps to top automatically how can I prevent it like I want to add data in virtual space I also tried with use of -

runOnUiThread(new Runnable() {
                                public void run() {
                                    innerAdapter.notifyDataSetChanged();
                                }
                            });

but it not working. how can I solve it?

add some code for scrolling up

if (isScrollUp) {
                            isEnable = true;
                            userChats.addAll(0, model.data);
                                                       innerChatAdapter.notifyDataSetChanged();
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    nnerChatAdapter.notifyItemInserted(0);
                                    recyclerView.smoothScrollToPosition(0);
                                }
                            }, 1);
                        }

Upvotes: 4

Views: 4490

Answers (3)

Ashish P
Ashish P

Reputation: 3056

Hello if you want to scroll down after add data to adapter.Add below method after notifyDataSetChanged() called.

 new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mRecyclerView.smoothScrollToPosition(new_data_size);
            }
        }, 1);

Upvotes: 6

Aashutosh Kumar
Aashutosh Kumar

Reputation: 183

To solve this problem you can use requestFocus() method on any other top element of your view.

  top_element.requestFocus();

Upvotes: 1

VinayagaSundar
VinayagaSundar

Reputation: 1701

Try this one Don't update the entire RecyclerView. Ref Here

runOnUiThread(new Runnable() {
    public void run() {
        innerAdapter.notifyItemInserted(position /* position of newly added item */);
    }
});

Upvotes: 1

Related Questions