在Android中如何滑动时获取BottomSheet的当前高度?

时间:2018-10-22 06:58:32

标签: android

当我在应用程序内部滑动持久性底部页面时,我想知道此时该底部页面的当前高度。我尝试调用BottomSheetCallback的onSlide方法,但是没有用。

    bottomSheetView = findViewById(R.id.bottom_sheet);
    bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView);
    bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if(BottomSheetBehavior.STATE_DRAGGING == newState)
                Log.i("MainActivity", "onStateChanged  >>  " + bottomSheet.getHeight());
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            Log.i("MainActivity", "onSlide  bottomSheet>>  " + bottomSheet.getHeight());
            Log.i("MainActivity", "onSlide  bottomSheetView>>  " + bottomSheetView.getHeight());
        }
    });

3 个答案:

答案 0 :(得分:0)

将ID赋予您底部工作表的父级布局,然后获取您的身高,例如, 父母是

 RelativeLayout relative  = findviewbyid (R.id.rel);
relative.getheight();

或根据您的状况以其他方式获得身高
您总是可以使用onWindowFocusChanged方法

获得布局的高度或宽度
@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

         int height = relative.getHeight();

     Toast.makeText(this, String.valueOf(height), Toast.LENGTH_SHORT).show();
}

答案 1 :(得分:0)

这是我下班的选择。

    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            int currentHeight = rootContainer.getHeight() - bottomSheet.getHeight();
            int bottomSheetShiftDown = currentHeight - bottomSheet.getTop();
            rootContainer.setPadding(
                    0,
                    0,
                    0,
                    (mBottomSheet.getPeekHeight() + bottomSheetShiftDown));
                }

答案 2 :(得分:0)

我认为滑动时高度不会改变,因为底片的实际视图没有改变。底页本身只是向下滑动。

但是您仍然可以使用bottomSheetView的 locationOnScreen 通过一些计算来获得高度。

例如:

bottomSheetBehavior.addBottomSheetCallback(object :
            BottomSheetBehavior.BottomSheetCallback() {
            override fun onSlide(bottomSheet: View, slideOffset: Float) {
                var locationOnScreen: IntArray = intArrayOf(0, 0)
                bottomSheet.getLocationOnScreen(locationOnScreen)
                Log.d(
                    "BottomSheet",
                    "slideOffset: $slideOffset - view height: ${locationOnScreen[0]},${locationOnScreen[1]}"
                )
            }

            override fun onStateChanged(bottomSheet: View, newState: Int) {
                Log.d("BottomSheet", "State: $newState")
            }

        })

BottomSheet向上/向下滑动时,locationOnScreen的值会不断变化。