本文主要講解實現(xiàn)微信朋友圈評論EditText效果思路,供大家參考,具體內(nèi)容如下
效果圖
當(dāng)我們點擊某一天朋友圈的評論是,列表也會跟隨著滑動,使得鍵盤剛好在我們點擊的那條評論上方
getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // 這里可以監(jiān)聽到鍵盤顯示與隱藏時界面可視區(qū)域的變化 Rect rect = new Rect(); View decorView = getWindow().getDecorView(); decorView.getWindowVisibleDisplayFrame(rect); int displayHeight = rect.bottom - rect.top; // 拿到鍵盤的高度,可能會有誤差,需要優(yōu)化 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) { // 拿到評論按鈕在屏幕中的坐標(biāo) 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在屏幕中的坐標(biāo) * @param commentView * @return */ private int getY(View commentView) { int[] outLocation = new int[2]; commentView.getLocationOnScreen(outLocation); return outLocation[1]; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點
疑難解答