最近項目中遇到問題:就是當軟鍵盤彈出,editext輸入時,遮擋listview,recyclerview條目,體驗效果不好;
網上搜集,驗證,找了個好的方法,希望能給大家項目中帶來幫助;
通過監聽editext計算軟鍵盤高度
通過這個方法即可獲得軟鍵盤高度,很簡單的。
et_comment.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//當鍵盤彈出隱藏的時候會 調用此方法。
@Override
public void onGlobalLayout() {
Rect r = new Rect();
//獲取當前界面可視部分
getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
//獲取屏幕的高度
int screenHeight = getWindow().getDecorView().getRootView().getHeight();
//此處就是用來獲取鍵盤的高度的, 在鍵盤沒有彈出的時候 此高度爲0 鍵盤彈出的時候爲一個正數
srfHeight = screenHeight - r.bottom;
if (srfHeight > 0) {
//讓listview滾動到指定條目距離頂部,1/3的軟鍵盤高度,正好在說說下方
lv_cir.getRefreshableView().setSelectionFromTop(cusPos + 1, -srfHeight / 3);
}
}
});