实现类似于微信朋友圈评论EditText效果

本文主要讲解实现思路

  • 效果图

这里写图片描述

 - 当我们点击某一天朋友圈的评论是,列表也会跟随着滑动,使得键盘刚好在我们点击的那条评论上方
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];
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值