我在键盘出现时使用android:windowSoftInputMode="stateHidden|adjustResize"
推送EditText
和RecyclerView,但它仅适用于EditText。 RecyclerView
保持在同一位置(因此某些元素不可见)。我做错了什么?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="co.bla.bla.ChatActivity"
android:id="@+id/chat_root">
<include android:id="@+id/toolbar" layout="@layout/toolbar"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/chat_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/message_input_wrapper"
android:scrollbars="vertical"
android:paddingBottom="20dp"
android:layout_below="@id/toolbar"
/>
<LinearLayout
android:id="@+id/message_input_wrapper"
android:layout_width="match_parent"
android:layout_height="60dp"
android:padding="6dp"
android:orientation="horizontal"
android:weightSum="4"
android:background="@android:color/white"
android:layout_alignParentBottom="true">
<EditText
android:id="@+id/message_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/chat_input_hint"
android:layout_gravity="center_vertical"
android:layout_weight="3"
android:background="@android:color/transparent"/>
<Button
android:id="@+id/message_send_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/chat_button_text"
android:layout_weight="1"
android:onClick="onButtonClicked"/>
</LinearLayout>
</RelativeLayout>
我不知道adjustResize
究竟是如何运作的,但我认为在这种情况下使用android:layout_above
时,RecyclerView
和LinearLayout
&#34;坚持& #34;在一起,应该一起推?
答案 0 :(得分:3)
在您的活动清单中:
android:windowSoftInputMode="adjustResize"
然后将OnLayoutChangeListener添加到yout recyclerView:
recyclerView.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)
{
recyclerView.scrollToPosition(recyclerView.getAdapter().getItemCount()-1);
}
});
这对我有用:)
答案 1 :(得分:0)
设置软键盘监听器:
private boolean mLastKeyBoardVisible = false;
private void addOnSoftKeyBoardVisibleListener(final OnSoftKeyBoardVisibleListener listener) {
final View decorView = getWindow().getDecorView();
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
int displayHight = rect.bottom - rect.top;
int hight = decorView.getHeight();
boolean visible = (double) displayHight / hight < 0.8;
if(visible != mLastKeyBoardVisible){
listener.onSoftKeyBoardVisible(visible);
}
mLastKeyBoardVisible = visible;
}
});
}
private OnSoftKeyBoardVisibleListener onSoftKeyBoardVisibleListener = new OnSoftKeyBoardVisibleListener() {
@Override
public void onSoftKeyBoardVisible(boolean visible) {
scrollToBottom(visible);
}
};
synchronized private void scrollToBottom(boolean visible) {
if (visible) {
if (mMessageList.size() != messageAdapter.getItemCount() && mMessageList.size() > 0) {
messageAdapter.notifyDataSetChanged();
messageListView.scrollToPosition(mMessageList.size() - 1);
} else if (mMessageList.size() > 0) {
messageListView.scrollToPosition(mMessageList.size() - 1);
}
}
}