我正在使用
BottomSheetDialogFragment
,我需要知道状态何时发生变化。
例如有
BottomSheetBehavior
的状态
PEEK_HEIGHT_AUTO - Peek at the 16:9 ratio keyline of its parent.
STATE_COLLAPSED - The bottom sheet is collapsed.
STATE_DRAGGING - The bottom sheet is dragging.
STATE_EXPANDED - The bottom sheet is expanded.
STATE_HIDDEN - The bottom sheet is hidden.
STATE_SETTLING - The bottom sheet is settling.
我想根据当前状态更改一些视图 - 例如,当
BottomSheetDialogFragment
将展开全屏时,我想在右上角显示十字图标以关闭它等..
我如何才能收听此类事件?
晚了,但可能是您问题的答案。你必须使用bottomSheetBehavior回调。
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(View bottomSheet, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_COLLAPSED:
Log.d("Bottom Sheet Behavior", "STATE_COLLAPSED");
break;
case BottomSheetBehavior.STATE_DRAGGING:
Log.d("Bottom Sheet Behavior", "STATE_DRAGGING");
break;
case BottomSheetBehavior.STATE_EXPANDED:
Log.d("Bottom Sheet Behavior", "STATE_EXPANDED");
break;
case BottomSheetBehavior.STATE_HIDDEN:
Log.d("Bottom Sheet Behavior", "STATE_HIDDEN");
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
break;
case BottomSheetBehavior.STATE_SETTLING:
Log.d("Bottom Sheet Behavior", "STATE_SETTLING");
break;
}
}
要从
BottomSheetBehaviour
获取 BottomSheetDialogFragment
,您必须在 BottomSheetDialogFragment.from(yourView)
的 onCreateView
方法中使用 BottomSheetDialogFragment
。
结果(Kotlin):
class MyFragment() : BottomSheetDialogFragment()
{
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View?
{
var result = inflater.inflate(R.layout.fragment_bitmap, container, false)
BottomSheetBehavior.from(result)
.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback()
{
override fun onStateChanged(bottomSheet: View,
newState: Int)
{
// Here
}
override fun onSlide(p0: View, p1: Float)
{
//
}
})
return result
}
}