Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
                        }
like image 972
Madhav_nimavat Avatar asked Feb 05 '23 07:02

Madhav_nimavat


1 Answers

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);
like image 145
ashish Avatar answered Feb 07 '23 12:02

ashish