Recyclerview automatically scrolls when data is inserted

I have one problem: when I add data to the list of arrays and then add them to the adapter after that, when I install it in the recycler view adapter, it automatically goes to the beginning, how can I prevent it, for example, I want to add data in virtual space, I also tried using -

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

but it does not work. how can i solve it?

add code to scroll

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);
                        }
+4
source share
3 answers

Hello, if you want to scroll down the page after adding data to the adapter. Add the method below after calling notifyDataSetChanged ().

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

RecyclerView. Ref Here

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

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

  top_element.requestFocus();
+1
source

Source: https://habr.com/ru/post/1679016/


All Articles