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

3

当我在我的应用程序中滑动持久底部工作表时,我想知道此时底部工作表的当前高度。我尝试通过调用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());
        }
    });

我遇到了同样的问题,你得到了任何解决方案吗? - geek919
4个回答

3

你说的“没用”是什么意思?这是我在滑动时计算当前高度的方法:

    private val bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback() {
        override fun onSlide(bottomSheet: View, slideOffset: Float) {
            drawerHeight = bottomSheet.height * slideOffset // Current height of bottom sheet
        }

        override fun onStateChanged(bottomSheet: View, newState: Int) = Unit
    }

1
我认为在滑动时高度不会改变,因为底部表的实际视图没有改变。底部表本身只是向下滑动。
但是你仍然可以通过对 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")
    }
})

当底部菜单向上/向下滑动时,locationOnScreen的值会不断变化。


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();
}

是的,也许吧,因为我没有看到你的代码,你的意思是什么不起作用?它给你 null 值吗? - Erfan
1
onWindowFocusChanged方法在底部菜单上下滑动时不会被连续调用。我想要在底部菜单上下滑动时获取高度。 - Tushski

0

这是我将Shift键向下移的选项。

    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));
                }

1
rootContainer.getHeight()和bottomSheet.getHeight()的值直到我释放底部面板才会改变。 - Tushski
getPeekHeight() 不要改变太多。 - Vadim Eksler
尝试记录bottomSheetShiftDown,您将看到从0到-bottomSheet.getHeight()的值,这是您向下移动时的高度 - bottomSheet.getHeight() + bottomSheetShiftDown。 - Vadim Eksler
当我尝试使用bottomSheet.getHeight() + bottomSheetShiftDown时,在向上滑动1/3的过程中,我得到了负值。 - Tushski
好的,rootContainer = 全屏高度 - 底部抽屉高度,这是我的第一行代码,你懂吗?当你向下滑动时,bottomSheet.getTop()必须增加。检查一下。 - Vadim Eksler
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接