简体   繁体   中英

How to make RecyclerView not scroll to the bottom each time an item is added to the list

I want my RecyclerView to be scrolled to the bottom when my chat loads up but when the user manually scrolls up the list to read previous messages it should not scroll back down again upon the addition of new item in the list unless the message(new item) is sent/added by the user himself.

I have tried using both layoutManager.setStackFromEnd(true) and recyclerView.scrollToPosition(list.size() - 1). Both of these methods automatically scroll down the list upon receiving a new message if I have scrolled up manually it should not scroll down to the bottom again unless im the one sending the message.

mref.child(RoomName).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()) {
            ChatMessage value = dataSnapshot1.getValue(ChatMessage.class);
            ChatMessage fire = new ChatMessage();
            String msgtxt = value.getMessageText();
            String user=value.getMessageUser();
            long msgtime=value.getMessageTime();
            String  prothumb=value.getProfuri();
            String sentimguri=value.getSentimguri();
            String type=value.getType();

            fire.setMessageUser(user);
            fire.setMessageText(msgtxt);
            fire.setMessageTime(msgtime);
            fire.setProfuri(prothumb);
            fire.setSentimguri(sentimguri);
            fire.setType(type);
            list.add(fire);
        }
        adapter = new RecyclerViewAdapter(ChatRoom.this, list);
        recyclerView.setAdapter(adapter);

        //layoutManager.setStackFromEnd(true);
    }

    //Initializing RecyclerView
    private void initRecyclerView() {
        //Log.d(TAG, "initRecyclerView: init recyclerview.");
        recyclerView =(RecyclerView) findViewById(R.id.list_of_messages);
        recyclerView.setHasFixedSize(true);
        layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        //recyclerView.scrollToPosition(list.size() - 1);
    }
}

The issue I can see is that you are creating a new instance of adapter everytime when data changes. Which explains why it always scroll down to bottom. And it's not a proper way of using a recyclerView.

What you need to do is to create just one instance of adapter during initRecyclerView using an empty list. Then whenever data changes, update the existing list and then call adapter.notifyDataSetChanged(). This will give you the behaviour that you wanted.

Upon getting the chat list , you need not have to create the new instance of adapter everytime .Instead just notify the adapter about the change .

mref.child(RoomName).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
       list.clear();
    for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()) {
        ChatMessage value = dataSnapshot1.getValue(ChatMessage.class);
        ChatMessage fire = new ChatMessage();
        String msgtxt = value.getMessageText();
        String user=value.getMessageUser();
        long msgtime=value.getMessageTime();
        String  prothumb=value.getProfuri();
        String sentimguri=value.getSentimguri();
        String type=value.getType();

        fire.setMessageUser(user);
        fire.setMessageText(msgtxt);
        fire.setMessageTime(msgtime);
        fire.setProfuri(prothumb);
        fire.setSentimguri(sentimguri);
        fire.setType(type);
        list.add(fire);
    }

  if(!(recyclerView.canScrollVertically(1)))
            {
            recyclerView.smoothScrollToPosition(adapter.getItemCount());
            }

    adapter.notifyDataSetChanged();
    //layoutManager.setStackFromEnd(true);
}

//Initializing RecyclerView
private void initRecyclerView() {
    //Log.d(TAG, "initRecyclerView: init recyclerview.");
    recyclerView =(RecyclerView) findViewById(R.id.list_of_messages);
    recyclerView.setHasFixedSize(true);
    layoutManager=new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    //initialse your adapter only once 
    adapter = new RecyclerViewAdapter(ChatRoom.this, list);        
    recyclerView.setAdapter(adapter);
    layoutManager.setStackFromEnd(true);

    //recyclerView.scrollToPosition(list.size() - 1);
}

}

This might will solve your issue .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM