RecyclerView中的EditText被软键盘遮挡的解决办法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
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);
}
}

}
});