如何在底部模态对话框中添加底部布局?

5
我希望使用 BottomSheetDialogFragment 实现这样的设计。但问题是当我拖动时,底部布局会滚动。我希望底部布局始终保持在底部,直到 BottomSheetDialogFragment 被关闭。

请查看截图:

ButtonSheetDialogFragment

这是我的代码:

bottomsheet_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--The main content goes over here-->
    </LinearLayout>

    <!--The footer view-->
    <LinearLayout
        android:id="@+id/footer_purchase_layout"
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        android:weightSum="1">

        <TextView
            android:id="@+id/txt_cancel_purchase"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:gravity="left|center_vertical"
            android:text="CLOSE"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/txt_item_purchase_action"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:gravity="right|center_vertical"
            android:text="ADD STICKERS"
            android:textSize="14sp" />

    </LinearLayout>
</FrameLayout>

ModalBottomSheetFragment.java

public class ModalBottomSheetFragment extends BottomSheetDialogFragment {

    public ModalBottomSheetFragment() {}

    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {

        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                dismiss();
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {}
    };

    @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
    @Override
    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);

        View mContentView = View.inflate(getContext(), R.layout.bottomsheet_layout, null);
        dialog.setContentView(mContentView);
        CoordinatorLayout.LayoutParams layoutParams =
                (CoordinatorLayout.LayoutParams) ((View) mContentView.getParent()).getLayoutParams();
        CoordinatorLayout.Behavior mBehavior = layoutParams.getBehavior();
        if (mBehavior != null && mBehavior instanceof BottomSheetBehavior) {
            ((BottomSheetBehavior) mBehavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
            int height = getScreenHeight(getActivity());
            final double desiredHeight = 0.85 * height;
            mContentView.getLayoutParams().height = height;
            ((BottomSheetBehavior) mBehavior).setPeekHeight((int) desiredHeight);

        }
    }

    public static int getScreenHeight(Context ctx) {
        DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
        return metrics.heightPixels;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Button mShowBottomSheetDialog;
    private BottomSheetDialogFragment bottomSheetDialogFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mShowBottomSheetDialog = (Button) findViewById(R.id.showBottomSheet);
        mShowBottomSheetDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomSheetDialogFragment = new ModalBottomSheetFragment();
                bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
            }
        });
    }
}
2个回答

0

请使用这个布局代替你的布局...

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:orientation="vertical">
        <!--The main content goes over here-->
    </LinearLayout>

     </ScrollView>

    <!--The footer view-->
    <LinearLayout
        android:id="@+id/footer_purchase_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="1">

        <TextView
            android:id="@+id/txt_cancel_purchase"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:gravity="left|center_vertical"
            android:text="CLOSE"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/txt_item_purchase_action"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:gravity="right|center_vertical"
            android:text="ADD STICKERS"
            android:textSize="14sp" />

    </LinearLayout>
    </LinearLayout>
</FrameLayout>

注意:将您的布局按照常量分成不同的视图......


实际问题是什么?你是否动态添加了任何滚动条? - sushildlh
是的,我也动态添加了。但是底部布局在拖动 BottomSheetDialog 时仍然会滚动。 - Ajay Shrestha
让我们在聊天中继续这个讨论 - sushildlh
@AjayShrestha你找到解决办法了吗?如果是的,请告诉我。需要紧急处理。 - Athul Antony
@AthulAntony 我没有使用底部工作表。我使用 https://github.com/umano/AndroidSlidingUpPanel 来实现这一点。尝试使用这个库,它会起作用的。 - Ajay Shrestha
显示剩余3条评论

0
使用这个布局:
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:actionBarSize"
        app:contentInsetEnd="0dp"
        app:contentInsetStart="0dp">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Title"
                android:textSize="18sp" />
        </FrameLayout>

    </androidx.appcompat.widget.Toolbar>


    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp">

            <!--your content here-->

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

    <FrameLayout
        android:id="@+id/bottomView"
        android:layout_width="match_parent"
        android:layout_height="70dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Footer"
            android:textSize="18sp"/>

    </FrameLayout>


</LinearLayout>

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