2017-06-16 75 views
1

我想創建一個簡單的底部表單佈局,一個按鈕顯示和其他摺疊,但它不工作。我正在關注this教程,但仍然沒有任何東西。所以我試圖傳遞按鈕內部的狀態,然後傳入回調方法來隱藏或展開,並且底部表單不能向下滾動!Android底部表單dos沒有摺疊

這是佈局。

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

回答

0

TextViewLinearLayout

0

我想該教程自己,當遇到同樣的問題。

您必須將BottomSheetBehavior設置爲hideable。此外,COLLAPSED狀態可能不是您要查找的內容。爲了使BottomSheet完全消失,您必須將狀態設置爲HIDDEN。像這樣:

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