1

我需要通过上下滑动来处理底部工作表视图的 3 种状态。例如,如果当前状态等于 STATE_EXPANDED,那么如果我向下滑动,状态应该更改为 STATE_HALF_EXPANDED,如果再次向下滑动 - STATE_COLLAPSED。这是我拥有的代码(我尝试使用 slideOffset,但没有成功):

BottomSheetBehavior.from(view.findViewById<LinearLayout>(R.id.bottom_sheet)).apply {
            peekHeight = Resources.getSystem().displayMetrics.heightPixels / 2
            this.state = BottomSheetBehavior.STATE_COLLAPSED
            this.addBottomSheetCallback(object :
                    BottomSheetBehavior.BottomSheetCallback() {

            override fun onSlide(bottomSheet: View, slideOffset: Float) {
                if(slideOffset <= 0.7) {
                    if(prevOffset - slideOffset > 0) {
                        BottomSheetBehavior.from(bottomSheet).state = BottomSheetBehavior.STATE_COLLAPSED
                    }
                    else {
                        BottomSheetBehavior.from(bottomSheet).state = BottomSheetBehavior.STATE_HALF_EXPANDED
                    }
                }
                else {
                    if(prevOffset - slideOffset > 0) {
                        BottomSheetBehavior.from(bottomSheet).state = BottomSheetBehavior.STATE_HALF_EXPANDED
                    }
                    else {
                        BottomSheetBehavior.from(bottomSheet).state = BottomSheetBehavior.STATE_EXPANDED
                    }
                }
                prevOffset = slideOffset
            }

            override fun onStateChanged(bottomSheet: View, newState: Int) {
                when(newState) {
                    BottomSheetBehavior.STATE_EXPANDED -> peekHeight = Resources.getSystem().displayMetrics.heightPixels
                    BottomSheetBehavior.STATE_HALF_EXPANDED -> peekHeight = (Resources.getSystem().displayMetrics.heightPixels * 0.7).toInt()
                    BottomSheetBehavior.STATE_COLLAPSED -> peekHeight = Resources.getSystem().displayMetrics.heightPixels / 2
                }
            }
        })
    }
4

0 回答 0