0

I am developing a chat app in android and its my first time. I have been able to list the messages from the adapter into the ChatActivity but it is not scrolling to the last message. The messages include both text and images. Any help please

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_chat);

        GroupNameRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
                if (snapshot.exists()){
                    DisplayMessages(snapshot);
                }
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
                if (snapshot.exists()){
                    DisplayMessages(snapshot);
                }
            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot snapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }
}

private void DisplayMessages(DataSnapshot snapshot) {
        Messages messages = snapshot.getValue(Messages.class);

        messagesList.add(messages);

        messageAdapter.notifyDataSetChanged();

        userMessagesList.scrollToPosition(userMessagesList.getAdapter().getItemCount() - 1);
    }

  • 1
    check this [link](https://stackoverflow.com/questions/26580723/how-to-scroll-to-the-bottom-of-a-recyclerview-scrolltoposition-doesnt-work). I hope it will help you. – Miro Jul 20 '22 at 07:13
  • I found this [question]:https://stackoverflow.com/questions/26580723/how-to-scroll-to-the-bottom-of-a-recyclerview-scrolltoposition-doesnt-work I hope it is useful for you. – Miro Jul 21 '22 at 06:44

0 Answers0