2016-07-12 32 views
5

我有一個底部頁面,裏面有一個NestedScrollView(見下文)。當我按FAB按鈕時,我想使此NestedScrollView中的某些部分不可見。但是當我將一些線性佈局的可見性改爲GONE時,底部片從頂部飛出。在這裏看到:底部頁面隨可見性變化而飛走

enter image description here

你可以從https://github.com/Tanrikut/BottomSheetExample

整個代碼我的變化可見性的方法:

private void changeVisibility() { 
    subtitleLayout.setVisibility(View.GONE); 

    coordinateLayout.setVisibility(View.GONE); 
    timeLayout.setVisibility(View.GONE); 

} 

我NestedScrollView XML:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView 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="wrap_content" 
    app:behavior_peekHeight="120dp" 
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior" 
    android:id="@+id/bottom_sheet_main"> 

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

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="28dp" 
      android:background="@android:color/white" 
      android:animateLayoutChanges="true" 
      android:orientation="vertical" 
      > 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:paddingLeft="10dp" 
       android:paddingStart="10dp" 
       android:paddingTop="@dimen/activity_horizontal_margin"> 

       <TextView 
        style="@style/TextAppearance.AppCompat.Headline" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Dandelion Chocolate" 
        android:id="@+id/title" /> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="@dimen/activity_horizontal_margin" 
        android:layout_marginTop="16dp" 
        android:orientation="horizontal" 
        android:id="@+id/subtitleLayout"> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:id="@+id/subtitle" 
         android:text="Subtitle" /> 

       </LinearLayout> 


      </LinearLayout> 


      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" 
       android:padding="@dimen/activity_horizontal_margin" 
       android:id="@+id/coordinateLayout"> 

       <ImageButton 
        android:layout_width="24dp" 
        android:layout_height="24dp" 
        android:alpha="0.36" 
        android:src="@drawable/ic_room_24dp" 
        android:background="@null" /> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="@dimen/activity_horizontal_margin" 
        android:layout_marginStart="@dimen/activity_horizontal_margin" 
        android:text="740, Valencia St, San Francisco, CA" 
        android:textColor="@android:color/primary_text_light" 
        android:id="@+id/bottom_sheet_coordinate" /> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" 
       android:padding="@dimen/activity_horizontal_margin" 
       android:id="@+id/timeLayout"> 

       <ImageButton 
        android:layout_width="24dp" 
        android:layout_height="24dp" 
        android:alpha="0.36" 
        android:src="@drawable/ic_query_builder_24dp" 
        android:background="@null" /> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="@dimen/activity_horizontal_margin" 
        android:layout_marginStart="@dimen/activity_horizontal_margin" 
        android:text="Wed, 10 AM - 9 PM" 
        android:textColor="@android:color/primary_text_light" 
        android:id="@+id/bottom_sheet_time" /> 

      </LinearLayout> 


     </LinearLayout> 

    </FrameLayout> 
</android.support.v4.widget.NestedScrollView> 
+0

你在哪裏調用changeVisibility()方法? – Lawrance

+0

在晶圓廠按鈕的clicklistener裏面 – Tanrikut

回答

0

可能是這樣可以幫你!我不能評論,以便張貼作爲一個答案

Here

for slide layout something same as bottom sheet but good

+0

我不清楚你的建議。你建議有多張底紙而不是改變一張底紙內的可見度? – Tanrikut

+0

它取決於你,但我建議一種出路代碼底部工作表,而不是你可以使用可滑動佈局看到我編輯的答案,因爲底部表是不是很好用(如我的觀點哈哈) –

0

BottomSheetBehavior有它自己的行爲,通過它可以得到相應的結果。以下是bottomomsheet的行爲。

STATE_DRAGGING,STATE_SETTLING,STATE_EXPANDED,STATE_COLLAPSED,STATE_HIDDEN

請勿使用任何佈局的可視性。

使用此行爲在你的代碼,如:

fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       int behaviorState = bottomSheetBehavior.getState(); 
       if (behaviorState == BottomSheetBehavior.STATE_EXPANDED) { 
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 
       } else { 
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); 
       } 
      } 
}); 
+0

我不想改變知名度底部表。我正在嘗試更改底部表單中可見的內容。 – Tanrikut

7

我碰到了這一點,花了一段時間來弄清楚究竟是什麼原因。

這是因爲您使用的是android:animateLayoutChanges,它會在BottomSheetBehavior或CoordinatorLayout中顯示一個錯誤。

刪除它,BottomSheet將停止它自己的動畫時,它不應該。不是一個修復,而是至少一個解決方法。

-

更新:

事實證明,如果你啓用了「animateLayoutChanges」通過編程設置LayoutTransition實例來使用,你可以設置一個標誌就可以了,這將阻止它,可欣賞那個搞亂你使用Android的一個祖先:animateLayoutChanges上(又名:你BottomSheet容器):

LayoutTransition transition = new LayoutTransition(); 
transition.setAnimateParentHierarchy(false); 
yourLinearLayoutThatNeedsLayoutAnimation.setLayoutTransition(transition); 
+0

這對我有用 –

+0

這正是解決問題的方法! - 謝謝! 這個問題目前仍然存在於當前的支持庫v25.0.1 – koesclem