Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push recyclerView up when keyboard appear?

I want to build a message layout like WhatsApp. I have an EditText and a RecyclerView.

The problem is when the keyboard appears, it hide the messages at the bottom of the list.

So let's say this the RecyclerView:

---item 1---  
---item 2---  
---item 3---  
---item 4---  
---EditText---

when the the keyboard appears, I get this:

---item 1---  
---item 2---  
---EditText---
---Keyboard---  

but I want to get this:

---item 3---  
---item 4---  
---EditText---
---Keyboard---

NOTE: when I set linearLayoutManager.setStackFromEnd(true); it works but when there is one message it appears at the bottom of the page.

like image 974
david Avatar asked Sep 10 '15 16:09

david


People also ask

How do you load all items in RecyclerView without scrolling?

It's pretty simple, simply set the RecyclerView 's height to wrap_content . That's right.

What is LayoutManager in RecyclerView?

This wear-specific implementation of LinearLayoutManager provides basic offsetting logic for updating child layout. A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user.

Can scroll vertically RecyclerView?

To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .


3 Answers

set adjustresize for the activity with recyclerview and editText :

android:windowSoftInputMode="adjustResize"

add onLayoutChangeListener to your RecyclerView and set scrollToPosition to data.size() -1 in onLayoutChange:

  mRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
 @Override

public void onLayoutChange(View v, int left, int top, int right,int bottom, int oldLeft, int oldTop,int oldRight, int oldBottom)
{

mRecyclerView.scrollToPosition(mMessages.size()-1);

}
    });
like image 78
devdoot Ghosh Avatar answered Oct 10 '22 06:10

devdoot Ghosh


The way I did it, is to set setStackFromEnd() according the size of the item holder, and set adjustResize in androidManifest.

This is how I check:

if (recycleView.getChildCount() == items.size()){
    mLayoutManager.setStackFromEnd(true);
}
else{
    mLayoutManager.setStackFromEnd(false);
}
like image 42
VHanded Avatar answered Oct 10 '22 08:10

VHanded


I am not good at English. Use ChatRecyclerView and set the stackfromend of the linearlayoutmanager to false.

public class ChatRecyclerView extends RecyclerView {
    private int oldHeight;

    public ChatRecyclerView(Context context) {
        super(context);
    }

    public ChatRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ChatRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        int delta = b - t - this.oldHeight;
        this.oldHeight = b - t;
        if (delta < 0) {
            this.scrollBy(0, -delta);
        }
    }
}
like image 2
minjung Ahn Avatar answered Oct 10 '22 07:10

minjung Ahn