Android底页dos没有崩溃

时间:2017-06-16 12:15:20

标签: java android bottom-sheet

我试图创建一个简单的Bottom Sheet布局,一个按钮显示和其他崩溃,但它无法正常工作。我正在关注this教程,但仍然没有。所以我试图在按钮内部传递状态,然后传入回调方法隐藏或展开,并且底部工作表无法滚动下来!

这是布局。

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_botton_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

        <Button
            android:id="@+id/button_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 1"
            android:padding="16dp"
            android:layout_margin="8dp"
            android:textColor="@android:color/white"
            android:background="@android:color/holo_green_dark"/>

        <Button
            android:id="@+id/button_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:layout_margin="8dp"
            android:text="Button 2"
            android:textColor="@android:color/white"
            android:background="@android:color/holo_blue_light"/>

        <Button
            android:id="@+id/button_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:layout_margin="8dp"
            android:text="Button 3"
            android:textColor="@android:color/white"
            android:background="@android:color/holo_red_dark"/>

    </LinearLayout>

</ScrollView>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="350dp"
    android:clipToPadding="true"
    android:background="@android:color/holo_orange_light"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="opaa"
        android:padding="16dp"
        android:textSize="16sp"/>

</android.support.v4.widget.NestedScrollView>

活动:

private View bottomSheet;
private Button button1,button2,button3;
private BottomSheetBehavior mBottomSheetBehavior;

bottomSheet = findViewById( R.id.bottom_sheet );
    button1 = (Button)findViewById(R.id.button_1);
    button2 = (Button)findViewById(R.id.button_2);
    button3 = (Button)findViewById(R.id.button_3);

    mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
    mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);

    mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
                mBottomSheetBehavior.setPeekHeight(0);
            }
            if(newState == BottomSheetBehavior.STATE_EXPANDED){
                mBottomSheetBehavior.setPeekHeight(500);
            }
        }

        @Override
        public void onSlide(View bottomSheet, float slideOffset) {
        }
    });
     button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    });
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        }
    });

2 个答案:

答案 0 :(得分:1)

我自己尝试该教程时遇到了同样的问题。

您必须将BottomSheetBehavior设置为hideable。此外,COLLAPSED州可能不是您想要的。要使BottomSheet完全消失,您必须将状态设置为HIDDEN。像这样:

mBottomSheetBehavior.setHideable(true);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

答案 1 :(得分:0)

TextView放入LinearLayout