3

Like in chat applications whenever we want to send messages soft keypad pop ups which also auto scroll the last seen messages to top of the soft keypad provided that nothing is hidden behind the soft keypad. But in my case the keypad hides the conversations. How to fix this issue and I have used to recycler view for displaying the messages. I used android.support.v7.widget.RecyclerView

bytecode77
  • 14,163
  • 30
  • 110
  • 141
Kalaiselvan
  • 408
  • 1
  • 6
  • 14

3 Answers3

4

With RecyclerView you can achieve this as follows:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(linearLayoutManager);

This block of code will tell the RecyclerView to scroll relative to the bottom of the list but also it shows the messages in reverse order so in your code if you are getting the messages from database, read them from the last message like this:

Cursor c = ...;
c.moveToLast();
do{
//your code which gets messages from cursor...
}while(c.moveToPrevoius());

and when you want to add a new message to the list just add them like this:

//ArrayList messages
messages.add(0, message);
Saeed Entezari
  • 3,685
  • 2
  • 19
  • 40
  • thank you... the first part of code partially fulfilled my expectation that when I start typing last seen messages comes above the keypad,till this its fine. But i also need when i send the message it should also should come at the last seen messages so that it comes like chat application like we send in whatsapp or other social messaging apps – Kalaiselvan Sep 21 '15 at 11:04
  • The second part is for that purpose. How are you adding your messages to the list? Can you show the code? @vigu – Saeed Entezari Sep 21 '15 at 11:11
2

How I handle with this is very simple, to achieve one good behaviour, similar to Whatsapp or other popular chats.

At first, set the LinearLayoutManager like this.

 new LinearLayoutManager(this.getContext(), LinearLayoutManager.VERTICAL, true);
 chatLayoutManager.setStackFromEnd(true);

When messages begin to overflow the RecyclerView at the bottom, needs to set the current position to 0, to achieve this, a simple interface works like a charm, to warning when the RecyclerView.Adapter has rendered the last message.

public interface OnLastItemRendered{

    void lastItemRendered();

}

Pass the interface implementation from the activity/fragment to de RecyclerView.Adapter to call scrollToPosition in the RecyclerView.

new ChatMessageListAdapter(
    context, user, lastOldMessages,
    new OnLastItemRendered() {

        @Override
        public void lastItemRendered() {

            ContainerFragment.this.myRecyclerViewToChat.scrollToPosition(0);
    }
});

And finally, if want to perform animations in the RecyclerView, implements one method to add the message in the RecyclerView.Adapter calling at the end to notifyItemInserted method.

public void add(ChatMessage chatMessage) {

    if (this.chatMessages == null) {

        this.chatMessages = new ArrayList<ChatMessage>();
    }

    this.chatMessages.add(chatMessage);

    Collections.sort(this.chatMessages, this.orderChatMessagesComparator);

    this.notifyItemInserted(0);

    this.onLastItemRendered.lastItemRendered();
}

And, for the X messages loaded at the beginning, in the onBindViewHolder.

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    final ChatMessage currentMessage = this.chatMessages.get(position);

    ... //Do your stuff.

    if (position == 0) this.onLastItemRendered.lastItemRendered();
}

Work done.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Dani
  • 4,001
  • 7
  • 36
  • 60
0

You can detect the focus on the edit text, and if your editText has focus, scroll your recyclerView list to the last element.

private OnFocuseChangeListener focuseListener = new OnFocuseChangeListener(){
  public void onFocusChange(View v, boolean hasFocus){
            if(hasFocus){

            // Scroll down to the last element of the list
            recyclerView.scrollToPosition(sizeOfYourList);

          } else {
              focusedView  = null;
        }
    }
}

Similarly use scrollToPosition () when you click on your send button or where you want to show the last element of the list.

Er.Rohit Sharma
  • 696
  • 5
  • 21
  • can you explain it elaborately I am a beginner in android developer.Actually i am using recycler view which have the list of chats and one edit text with send button. when i types the messages keypad hides the chats so those chats should autoscroll above keypad... – Kalaiselvan Sep 21 '15 at 10:12
  • Simply you have to setOnfocusChangeListener on your editText where your are entering the text message. Like: editText.setOnFoucsChangeListener(focuseListener); then use above code to detect the focus on your editText. Each time you touch your editText, it gains focus, and each time it call the focusListener in the above code. So, call recyclerView.ScrollToPosition(sizeOfYourList); method. It will lead you to the last row of the list. Similarly call this method of the click of your send button. – Er.Rohit Sharma Sep 21 '15 at 10:57