问题:ScrollView,键盘弹出后,视图会自动上移,RecyclerView却不会。
解决方法一:
RecyclerView:
1.在当前的manifest的对应的activity属性中添加
android:windowSoftInputMode="stateHidden|adjustPan"
2.设置layoutManager:
mLayoutManager.setStackFromEnd(true);
以上设置后,如果列表不满,会有问题,解决方法二:
mRecyclerView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (bottom < oldBottom) {
mRecyclerView.post(new Runnable() {
@Override
public void run() {
if (mAdapter.getItemCount() > 0) {
mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount() - 1);
}
}
});
}
}
});
以上方法存在问题:键盘会遮挡一部分布局,无法拖动到底部,最终方案:
1.添加键盘处理工具类的模块依赖:
链接: keyboard-panel-lib.zip.
2.布局中添加键盘占位布局
<cn.dreamtobe.kpswitch.widget.KPSwitchFSPanelLinearLayout
android:id="@+id/report_panel_root"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical"
android:visibility="gone" />
3.添加键盘监听
private var globalLayoutListener: ViewTreeObserver.OnGlobalLayoutListener? = null
private fun addKeyboardShowListener() {
globalLayoutListener = KeyboardUtil.attach(ourActivity, report_panel_root) { isShowing ->
if (isShowing) {
report_panel_root.visibility = View.VISIBLE
rlReportKeyBoardShow.visibility = View.GONE
} else {
KPSwitchConflictUtil.hidePanelAndKeyboard(report_panel_root)
rlReportKeyBoardShow.visibility = View.VISIBLE
}
}
}