android - 从 BottomSheetDialog 获取 BottomSheetBehaviour

标签 android android-layout grid-layout bottom-sheet

这是我的 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>

这是我创建 Bottom Sheet 对话框的方式:

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

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

只是为了让一切都清楚: 无论 Bottom Sheet 的状态如何,每次都应启用 GridView 滚动。

最佳答案

Grid view scroll should be enabled everytime irrespective of the state of bottom sheet.

我认为您不应该这样做,因为 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){

    }
};

关于android - 从 BottomSheetDialog 获取 BottomSheetBehaviour,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37322251/

相关文章:

android - 为什么 LinearLayout 看起来像这样(元素之间的空白)?

android - 我的 shell 脚本没有在 android 上运行

android:layout_width ="Xdp"不工作

java - 如何以编程方式访问嵌套布局中的 View ?

wpf - 网格面板 WPF 中的拖放重新排序控件

android - getContactsFromFirebase() 方法返回一个空列表

Android "opening ripple"效果

html - 严格的 2 列布局与元素跨越多列的 12 列相比有哪些优势?

Java制作四子棋游戏面板

android - 如何使用requestSync延迟syncAdapter的执行