从BottomSheetDialog获取BottomSheetBehaviour

这是我的BottomSheetDialog的布局。 我也有一个网格布局。 网格布局的滚动是不正确的。 我的意思是它只能在BottomSheetDialog的扩展状态下滚动。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:layout_behavior="@string/bottom_sheet_behavior" > <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="@dimen/view_padding_medium" android:gravity="center_vertical"> <ImageView android:layout_width="@dimen/profile_image" android:layout_height="@dimen/profile_image" android:src="@drawable/icon" /> <TextView android:id="@+id/title1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/view_padding_medium" android:text="@string/smart_action_share" android:textColor="@color/white" /> </LinearLayout> <GridView android:id="@+id/gridView11" android:layout_width="match_parent" android:layout_height="wrap_content" android:numColumns="3" > </GridView> </LinearLayout> 

这是我创建底部表单对话框的方式:

 BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context); LayoutInflater inflater = ((Activity) Constants.getContext()).getLayoutInflater(); View view = inflater.inflate(R.layout.dialog_share1, null); bottomSheetDialog.setContentView(view); final GridView grid = (GridView) view.findViewById(R.id.gridView11); CustomAdapter adapter = new CustomAdapter (context); grid.setAdapter(adapter); bottomSheetDialog.show(); 

如何访问对话框的行为,以便我可以修复网格布局滚动或有任何其他方式修复?

只是为了清楚一切:不管底部页面的状态如何,每次都应该启用网格视图滚动。

不管底部页面的状态如何,每次都应该启用网格视图滚动。

我不认为你应该这样做,因为BottomSheet的高度应该匹配内容的高度。

这意味着,如果内容是可滚动的并且超过了父母的身高,那么只有当BottomSheet由于默认行为而被展开时,滚动才会起作用,这是合理的。

要访问该行为,您可以执行以下操作:

 View view = inflater.inflate(R.layout.dialog_share1, null); bottomSheetDialog.setContentView(view); BottomSheetBehavior behavior = BottomSheetBehavior.from((View) view.getParent()); 

然后,自定义不同于展开状态的行为:

 behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, @BottomSheetBehavior.State int newState) { if (newState == BottomSheetBehavior.STATE_HIDDEN) { dismiss(); }else{ if (newState != BottomSheetBehavior.STATE_EXPANDED) { // Implement your logic here } } } @Override public void onSlide(@NonNull View bottomSheet, float slideOffset){ } }; 

你可以做(​​Kotlin):

  val params = ((view.parent as View).layoutParams as CoordinatorLayout.LayoutParams) val behavior = params.behavior if (behavior is BottomSheetBehavior) { behavior.doStuff() } 
Interesting Posts