AndroidManifest.xml中相关activity设置
android:windowSoftInputMode="stateUnchanged|adjustResize"
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_chat_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/colorAccent"
android:visibility="gone" />
<LinearLayout
android:id="@+id/keyboard_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/lv_chatting"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:cacheColorHint="#00000000"
android:listSelector="#00000000"
android:scrollbarStyle="outsideOverlay"
android:transcriptMode="normal" />
<LinearLayout
android:id="@+id/input_all_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:minHeight="42dp"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingTop="6dp"
android:paddingRight="10dp"
android:paddingBottom="6dp">
<EditText
android:id="@+id/chat_input_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="3dp"
android:layout_marginRight="6dp"
android:layout_weight="1"
android:background="@drawable/bg_chat_input"
android:clickable="true"
android:hint="请输入短信"
android:maxLines="3"
android:minHeight="34dp"
android:paddingLeft="10dp"
android:paddingTop="7dp"
android:paddingRight="10dp"
android:paddingBottom="7dp"
android:textColorHint="#cecaca"
android:textSize="16sp" />
<ImageButton
android:id="@+id/add_menu_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:adjustViewBounds="true"
android:background="@drawable/icon_more"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
activity中核心代码
keyboardLayout.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) {
if(oldBottom != -1 && oldBottom > bottom){
recyclerView.requestLayout();
recyclerView.post(new Runnable() {
@Override
public void run() {
recyclerView.scrollToPosition(mAdapter.getItemCount() - 1);
}
});
}
}
});
注意!!!
注意!!!
注意!!!
聊天界面在fragment中设置无效