2017-04-08 108 views
1

的孩子,這是我的XML佈局名稱的SongList:BottomSheetBehavior不CoordinatorLayout

enter image description here

<android.support.design.widget.CoordinatorLayout 
    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="match_parent"> 

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

     <LinearLayout 
      android:id="@+id/viewA" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="0.6" 
      android:background="@android:color/holo_purple" 
      android:orientation="horizontal"/> 

     <android.support.v4.widget.NestedScrollView 
      android:id="@+id/bottom_sheet" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:color/holo_blue_bright" 
      app:layout_behavior="android.support.design.widget.BottomSheetBehavior" 
      > 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
       <ListView 
        android:id="@+id/list" 
        android:layout_width="match_parent" 
        android:layout_height="308dp" 
        /> 
      </LinearLayout> 

     </android.support.v4.widget.NestedScrollView> 
    </LinearLayout> 
    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:clickable="true" 
     android:src="@drawable/personlog" 
     app:layout_anchor="@id/viewA" 
     app:layout_anchorGravity="bottom|center"/> 

</android.support.design.widget.CoordinatorLayout> 

,這是我的片段,其中包含此佈局:

public class SongList extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.songlist,container,false); 

     textView=(TextView)view.findViewById(R.id.txt); 

     View bottomSheet = view.findViewById(R.id.bottom_sheet); 
     BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); 
     bottomSheetBehavior.setPeekHeight(200); 
return view;} 
} 

但是當午餐的應用程序給我這個錯誤:

java.lang.IllegalArgumentException: The view is not a child of CoordinatorLayout 

從這一行:

BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); 

如何解決這一問題?似乎所有的東西很好地工作,但給該錯誤......如果任何一個可以請幫助

+0

那麼如何才能使這種佈局真正的方式?如果你有一些解決方案,請回答 – Erf

+0

我添加NestedScrollView裏面CoordinatorLayout但仍然相同的問題 – Erf

+0

可以理解你的觀點。如果你想幫助我,請回答我的問題,不要評論,然後告訴我哪裏的代碼必須改變到什麼或那樣的事情。 TNX – Erf

回答

3

BottomSheetBehavior

爲CoordinatorLayout的子視圖,使其作爲一個底層工作的互動行爲插件。

目前您的底部工作表NestedScrollViewLinearLayout的子女。所以完全刪除最外面的LinearLayout

<android.support.design.widget.CoordinatorLayout 
    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="match_parent"> 

    <LinearLayout 
     android:id="@+id/viewA" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="0.6" 
     android:background="@android:color/holo_purple" 
     android:orientation="horizontal"/> 

    <android.support.v4.widget.NestedScrollView 
     android:id="@+id/bottom_sheet" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_blue_bright" 
     app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> 

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

      <ListView 
       android:id="@+id/list" 
       android:layout_width="match_parent" 
       android:layout_height="308dp" /> 
     </LinearLayout> 
    </android.support.v4.widget.NestedScrollView> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:clickable="true" 
     android:src="@drawable/personlog" 
     app:layout_anchor="@id/viewA" 
     app:layout_anchorGravity="bottom|center" /> 
</android.support.design.widget.CoordinatorLayout> 

但是現在你還有一些問題需要處理,首先,您不應該使用帶滾動視圖的wrap_content。其次,你不應該在滾動視圖中使用列表視圖,因爲它實現了自己的滾動。您可以通過僅使用列表視圖作爲底部表單來簡化此操作。

相關問題