问题来源
最近写发布器遇到一个恶心问题:在RecyclerView中的EditText被软键盘遮挡,这玩意儿弄了一上午
单纯的一个Item中只有一个EditText,弹起软键盘是完全没问题的
但是我的一个Item中上面是ImageView,下面是一个EditText,像这样:
![]()
这个用户体验是超级差的的,这个问题不关乎WindowSoftInoutMode的事情
想了想也查了一些资料,只能来个全局监听了
上代码:
getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mImgKeyboard.setImageResource(KeyboardUtils.isSoftShowing(ForumsPublishPostActivity.this)
? R.mipmap.jianpan_down_ico : R.mipmap.jianpan_ico);
// 除了软键盘以外的可见区域
Rect rect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
// 计算出剩余高度: 除了状态栏高度、topBar高度、bottomBar高度、键盘高度的剩余高度
int invisibleHeight = rect.bottom
- ViewUtils.getSystemBarHeight(ForumsPublishPostActivity.this)
- ViewUtils.dp2px(ForumsPublishPostActivity.this, 44)
- ViewUtils.dp2px(ForumsPublishPostActivity.this, 44);
// 计算出所点击的图片描述的EditText距离RecyclerView顶部的距离
View etDescView = mRecyclerview.getLayoutManager().findViewByPosition(mAdapter.etFocusPosition);
if (etDescView != null) {
int focusViewTop = etDescView.getTop();
int itemHeight = etDescView.getHeight();
int differ = focusViewTop + itemHeight - invisibleHeight;
if (differ > 0) {
// 让RecyclerView滚动差的那点距离
mRecyclerview.scrollBy(0, differ);
}
}
}
});