Android 仿UC浏览器详情页评论弹框效果

大概就这种效果

额,突然发现UC被我卸载了,这个是QQ浏览器的效果,不过都一样,如果当前页面不是全屏的话,把根布局设为相对布局,然后设置评论布局为处于底部,这样在点击评论时弹开键盘会触发布局重绘,底部的评论也会处于软键盘的上方,键盘消息,布局下移,但是我们要说的是全屏的情况下我们如何处理。
首先要监听到软件盘弹起,然后再设置评论的popupWindow

import android.app.Activity;
import android.graphics.Rect;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewTreeObserver;

public class KeyboardStatusDetector {

    private static final int SOFT_KEY_BOARD_MIN_HEIGHT = 100;
    private KeyboardVisibilityListener mVisibilityListener;

    boolean keyboardVisible = false;

    public KeyboardStatusDetector registerFragment(Fragment f) {
        return registerView(f.getView());
    }

    public KeyboardStatusDetector registerActivity(Activity a) {
       return registerView(a.getWindow().getDecorView().findViewById(android.R.id.content));
    }

    public KeyboardStatusDetector registerView(final View v) {
        v.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                v.getWindowVisibleDisplayFrame(r);
                //int heightDiff = v.getRootView().getHeight() - (r.bottom - r.top);
                int heightDiff = v.getRootView().getHeight() - r.bottom;
                LogUtils.e("键盘A--->", v.getRootView().getHeight() +"   "+ r.bottom +"  "+ r.top);
                if (heightDiff > SOFT_KEY_BOARD_MIN_HEIGHT) { // if more than 100 pixels, its probably a keyboard...
                    if (!keyboardVisible) {
                        keyboardVisible = true;
                        if (mVisibilityListener != null) {
                            mVisibilityListener.onVisibilityChanged(true, heightDiff);
                        }
                    }
                } else {
                    if (keyboardVisible) {
                        keyboardVisible = false;
                        if (mVisibilityListener != null) {
                            mVisibilityListener.onVisibilityChanged(false, heightDiff);
                        }
                    }
                }
            }
        });

        return this;
    }

    public KeyboardStatusDetector setVisibilityListener(KeyboardVisibilityListener listener) {
        mVisibilityListener = listener;
        return this;
    }

    public interface KeyboardVisibilityListener {
        void onVisibilityChanged(boolean keyboardVisible, int heightDiff);
    }
}

然后再对popupWindow进行设置位置

private void showSendMsgPop() {
        if (null == sendMsgPopup) {
            sendMsgPopup = new SendMsgPopupWindow(NewsDetailsActivity.this);
        }
      //设置处于底部
        sendMsgPopup.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }

当然,最后最关键的是popupWindow里的配置,要不然popupWindow会被软键盘遮挡。

@Override
    public void dismiss() {
        super.dismiss();
        // 评论框消息的同时把软键盘关闭
        KeyBoardUtils.closeKeybord(editText, mContext);
    }

    public void showAtLocation(View parent, int gravity, int x, int y) {
       // 这三行配置是关键
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); // 在显示输入法之后调用,否则popupwindow会在窗口底层
        super.showAtLocation(parent, gravity, x, y);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值