随软件盘弹出而升起相关操作

2020-03-28  本文已影响0人  今天也要努力呀y

1.Recyclerview升起
linearLayoutManager.setStackFromEnd(true);
2.某一个布局升起
工具类


public class WithSoftUp {
    public static void addLayoutListener(final View main, final View scroll) {
        main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override

            public void onGlobalLayout() {

                Rect rect = new Rect();

                main.getWindowVisibleDisplayFrame(rect);

                int screenHeight = main.getRootView().getHeight();

                int mainInvisibleHeight = main.getRootView().getHeight() - rect.bottom;

                if (mainInvisibleHeight > screenHeight / 4) {

                    int[] location = new int[2];

                    scroll.getLocationInWindow(location);

                    int srollHeight = (location[1] + scroll.getHeight()) - rect.bottom;

                    main.scrollTo(0, srollHeight);

                } else {

                    main.scrollTo(0, 0);

                }

            }

        });


    }
}

使用:

        WithSoftUp.addLayoutListener(findViewById(R.id.parent), linearLayout);
上一篇下一篇

猜你喜欢

热点阅读