软键盘隐藏监听以及eventbus部分使用

@Override
    protected void onResume()
    {
        super.onResume();
        mChildOfContent = activityRootView.getChildAt(0);
        //添加layout大小发生改变监听器
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
        {
            public void onGlobalLayout()
            {
                possiblyResizeChildOfContent();
            }
        });
    }

int usableHeightPrevious;

private void possiblyResizeChildOfContent()
{
    int usableHeightNow = computeUsableHeight();
    if (usableHeightNow != usableHeightPrevious)
    {
        int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
        int heightDifference = usableHeightSansKeyboard - usableHeightNow;
        if (heightDifference > (usableHeightSansKeyboard / 4))
        { // 键盘弹出
            Logger.d("~~~~~frag~~~~~~键盘弹出~~~~~~~~~~~~~~");
            EventBus.getDefault().post((Boolean) false);
        } else
        { // 键盘收起
            Logger.d("~~~~frag~~~~~~~键盘受~~~~~~~~~~~~~~");
            EventBus.getDefault().post((Boolean) true);
        }
        mChildOfContent.requestLayout();
        usableHeightPrevious = usableHeightNow;
    }
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(Boolean msg)
{
    if (msg)
        Logger.d("~~~~frag~~~~1~~~键盘受~~~~~~~~~~~~~~");
    else
        Logger.d("~~~~~frag~~~1~~~键盘弹出~~~~~~~~~~~~~~");
}

private int computeUsableHeight()
{
    Rect r = new Rect();
    mChildOfContent.getWindowVisibleDisplayFrame(r);
    return (r.bottom - r.top);
}

 

你可能感兴趣的:(懒癌笔记)