There is a RecyclerView in which elements can be added to the beginning and end of the list.

<android.support.v7.widget.RecyclerView android:id="@+id/recylerview_message_list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginBottom="6dp" app:layout_constraintBottom_toTopOf="@id/view" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/main_appBarLayout"> 

When added to the beginning, everything is OK, but when added to the end, everything will automatically scroll up 4 elements.

Initialize recyclerView:

  @Override public void showMessages(List<MessageModel> messages) { chatMessages = messages; Collections.sort(chatMessages); linearLayoutManager = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false); recyclerView.setAdapter(messageListAdapter); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setOnScrollListener(new EndlessRecyclerViewScrollListener(linearLayoutManager, this)); linearLayoutManager.setStackFromEnd(true); chatLoadingProgressBar.setVisibility(View.INVISIBLE); view.setVisibility(View.VISIBLE); layoutChatBox.setVisibility(View.VISIBLE); } 

Adding a new item (Commenting out what I have already tried)

 @Override public void addMessage(MessageModel message) { //List<MessageModel> oldMessages = new ArrayList<>(chatMessages); chatMessages.add(message); //linearLayoutManager.onRestoreInstanceState(recyclerViewState); //recyclerView.setAdapter(messageListAdapter); //messageListAdapter.notifyItemRangeChanged(0, chatMessages.size()); //messageListAdapter.notifyItemRangeChanged(messageListAdapter.getItemCount(), chatMessages.size()); //messageListAdapter.notifyDataSetChanged(); //messageListAdapter.notifyItemInserted(chatMessages.size()); //DiffUtil.DiffResult result = DiffUtil.calculateDiff(new CustomDiffUtilCallback(old, chatMessages)); //result.dispatchUpdatesTo(messageListAdapter); } 

How to disable automatic scrolling when adding items to the end of the list? When added to the beginning, everything works with messageListAdapter.notifyItemInserted(0);

  • Try to save the current position, after adding a new element - put the scroll to the previous position - well, it's like a crutch option. - Georgiy Chebotarev

1 answer 1

maybe this is the answer to your

Use recyclerView.smoothScrollToPosition

 recyclerView.smoothScrollToPosition(recyclerView.getAdapter().getItemCount() - 1); 
  • I don’t need to go to the bottom, I don’t have to scroll - Simatsu Edgeworth