本文主要讲解实现思路

- 当我们点击某一天朋友圈的评论是,列表也会跟随着滑动,使得键盘刚好在我们点击的那条评论上方
getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
View decorView = getWindow().getDecorView();
decorView.getWindowVisibleDisplayFrame(rect);
int displayHeight = rect.bottom - rect.top;
keyboardHeight = decorView.getHeight() - displayHeight;
if (displayHeight * 1.0 / decorView.getHeight() > 0.8) {
dialog.dismiss();
}
}
});
- 考虑到评论的EditText是可以隐藏的,所以把它写到Dialog中,初始化Dialog的代码就不贴出来了
- 点击弹出Dialog
private void showInputComment(View commentView, final int position) {
final int rvInputY = getY(commentView);
final int rvInputHeight = commentView.getHeight();
dialog.show();
handler.postDelayed(new Runnable() {
@Override
public void run() {
int dialogY = getY(dialog.findViewById(R.id.dialog_layout_comment));
rv.smoothScrollBy(0, rvInputY - keyboardHeight + dialogY + rvInputHeight);
}
}, 300);
}
/**
* 拿到View在屏幕中的坐标
* @param commentView
* @return
*/
private int getY(View commentView) {
int[] outLocation = new int[2];
commentView.getLocationOnScreen(outLocation);
return outLocation[1];
}