好記性不如爛筆頭。
1. 在AndroidManifest.xml 中添加屬性
<activity android:name=".chat.ChatActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden" />
2.根佈局layout中添加屬性 android:fitsSystemWindows="true"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
xmlns:tools="http://schemas.android.com/tools">
3. 點擊Recyclerview 鍵盤消失
public class TouchCloseInfutSoftMethodFrameLayout extends FrameLayout {
public TouchCloseInfutSoftMethodFrameLayout(@NonNull Context context) {
super(context);
}
public TouchCloseInfutSoftMethodFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public TouchCloseInfutSoftMethodFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
KeyboardUtils.hideSoftInput(this);
super.dispatchTouchEvent(ev);
break;
default:
super.dispatchTouchEvent(ev);
}
return true;
}
}
<com.oversea.commonmodule.widget.TouchCloseInfutSoftMethodFrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingTop="8dp"
android:paddingBottom="16dp"
android:layout_above="@+id/layout_bottom"
android:layout_below="@+id/layout_top">
<com.oversea.commonmodule.widget.recyclerview.LoadMoreRecyclerView
android:id="@+id/lmr_message_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.oversea.commonmodule.widget.TouchCloseInfutSoftMethodFrameLayout>
4.私聊頁面查看歷史消息,判斷是否到頂部
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (!recyclerView.canScrollVertically(-1)) {
DLog.d(TAG, " loadMore start");
presenter.loadMore();
}
}
});
5. 私聊頁面消息的添加
@Override
public void loadMessage(List<ChatMsgEntity> mList, boolean firstLoad) {
if(mList == null || mList.size() == 0){
return;
}
DLog.d(TAG, " loadMore loadMessage firstLoad = " + firstLoad +
" thread = " + Thread.currentThread().getName());
if(firstLoad){
mAdapter.replaceData(mList);
mRecyclerView.scrollToPosition(mDatas.size() - 1);
}else{
mDatas.addAll(0,mList);
mAdapter.notifyItemRangeInserted(0,mList.size());
mRecyclerView.scrollToPosition(mList.size() - 1);
}
}
6.當用戶滑動列表到中間,再次點擊輸入框。這時候需要將recycler view 滾動到最後一行。
//底部變化後,滾動recylerview
findViewById(R.id.layout_bottom).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 (left == oldLeft && right == oldRight && oldTop > top && oldBottom > bottom) {
mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1);
}
}
});