监听布局变化 ,调整布局

final ViewTreeObserver.OnGlobalLayoutListener layoutListener
                = () -> {
            if (!editTextInited) {
                LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) post_content.getLayoutParams();
                editTextHeight = post_content.getBottom() - post_content.getTop();
                p.weight = 0;
                p.height = editTextHeight;
                post_content.setLayoutParams(p);
                editTextInited = true;
            }

            Rect r = new Rect();
            //获取当前界面可视部分
            view.getWindowVisibleDisplayFrame(r);
            //获取屏幕的高度
            int screenHeight = view.getRootView().getHeight();
            int height = screenHeight - r.bottom;
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) post_content.getLayoutParams();
            Log.d(TAG, "screenHeight: " + screenHeight);
            Log.d(TAG, "r.bottom: " + r.bottom);
            Log.d(TAG, "move: " + move);
            Log.d(TAG, "post_content.height: " + params.height);
            Log.d(TAG, "post_content.高度: " + (post_content.getBottom() - post_content.getTop()));
            if (height == 0) {
                if (move) {
                    params.height = editTextHeight;
                    post_content.setLayoutParams(params);
                    move = false;
                    Log.d(TAG, "onGlobalLayout: " + "展开");
                }
            } else {
                if (!move) {
                    params.height = editTextHeight - height;
                    post_content.setLayoutParams(params);
                    move = true;
                    Log.d(TAG, "onGlobalLayout: " + "收起");
                }
            }

        };
        view.getRootView().getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);

你可能感兴趣的:(监听布局变化 ,调整布局)