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

本文介绍了一个在RecyclerView中EditText被软键盘遮挡的问题解决方案。通过全局监听软键盘状态,计算并调整RecyclerView滚动,确保EditText始终可见,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题来源

最近写发布器遇到一个恶心问题:在RecyclerView中的EditText被软键盘遮挡,这玩意儿弄了一上午

单纯的一个Item中只有一个EditText,弹起软键盘是完全没问题的

但是我的一个Item中上面是ImageView,下面是一个EditText,像这样:

          

这个用户体验是超级差的的,这个问题不关乎WindowSoftInoutMode的事情

想了想也查了一些资料,只能来个全局监听了

上代码:


        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);
                    }
                }

            }
        });

完美解决

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值