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.
It's pretty simple, simply set the RecyclerView 's height to wrap_content . That's right.
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.
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 .
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);
}
});
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);
}
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With