Android - CoordinatorLayout

时间:2016-08-20 23:14:36

标签: android xml android-fragments android-coordinatorlayout bottom-sheet

首先我不熟悉CoordinatorLayouts,我有一个包含几个可滚动的CardViews的片段,当我尝试添加底部工作表时,我得到了底部工作表必须是一个CoordinatorLayout的孩子错误,所以我添加了一个coordinatorLayout,现在我似乎无法滚动了

PS:我尝试用ScrollView替换NestedScrollView并且它不起作用,我甚至尝试编辑XML标记顺序,但没有解决它,我仍然无法滚动

我是否可以使用Bottom Sheets而无需使用CoordinatorLayout

这是使用ScrollView的XML布局片段:

<android.support.design.widget.CoordinatorLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 tools:context=".ThreeFragment" >
 <ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="false">
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical"
        android:padding="10dp">
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dp"
            style="@style/MyCardViewStyleTitle"
            android:id="@+id/view">
            <TextView
                android:text="I- Pure Vowels"
                android:textStyle="normal"
                android:background="@color/colorPrimaryDark"
                android:textSize="20dp"
                android:textColor="@color/white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_marginBottom="1dp"
            android:layout_height="wrap_content"
            style="@style/MyCardViewStyleContent"
            android:id="@+id/textView10"
            android:layout_below="@+id/view"
            android:layout_alignParentStart="true">
            <TextView
                android:text="Pronouncing : á"
                android:textStyle="bold|italic"
                android:textSize="17dp"
                android:textColor="@color/black"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:text="\n\nas in English father\nE.g.  ár ‘year’"
                android:textStyle="normal"
                android:textSize="40px"
                android:textColor="@color/black"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </android.support.v7.widget.CardView>
      <!-- more cardviews -->
    </LinearLayout>
</ScrollView>
 <!-- Bottom Sheet Layout-->
 <include layout="@layout/learn_more_sheet" />
</android.support.design.widget.CoordinatorLayout>

以下是使用NestedScrollView的XML布局片段:

<android.support.design.widget.CoordinatorLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 tools:context=".ThreeFragment" >
 <android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="false">
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical"
        android:padding="10dp">
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dp"
            style="@style/MyCardViewStyleTitle"
            android:id="@+id/view">
            <TextView
                android:text="I- Pure Vowels"
                android:textStyle="normal"
                android:background="@color/colorPrimaryDark"
                android:textSize="20dp"
                android:textColor="@color/white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_marginBottom="1dp"
            android:layout_height="wrap_content"
            style="@style/MyCardViewStyleContent"
            android:id="@+id/textView10"
            android:layout_below="@+id/view"
            android:layout_alignParentStart="true">
            <TextView
                android:text="Pronouncing : á"
                android:textStyle="bold|italic"
                android:textSize="17dp"
                android:textColor="@color/black"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:text="\n\nas in English father\nE.g.  ár ‘year’"
                android:textStyle="normal"
                android:textSize="40px"
                android:textColor="@color/black"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </android.support.v7.widget.CardView>
      <!-- more cardviews -->
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>
 <!-- Bottom Sheet Layout-->
 <include layout="@layout/learn_more_sheet" />
</android.support.design.widget.CoordinatorLayout>

主要活动XML布局

<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"
  xmlns:tools="http://schemas.android.com/tools"
  tools:context="com.incorp.anisvikernes.englishtonorse.MainActivity">
>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="160px"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:1)

编辑:从片段中删除BottomSheetLayout,并将其放在ViewPager下的Activity中。然后在片段顶部放置一个NestedScrollView,其属性如下:

<android.support.v4.widget.NestedScrollView 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:fillViewport="true">

要使BottomSheet仅出现在片段中,您可以这样做: 将可见性设置为android:visiblity="gone",并在片段中将其设置为visibile,如下所示:

var sheet = (View)getActivity().findViewById(R.id.{sheet});
sheet.setVisbility(view.Visible);

然后,在其他片段中,使用sheet.setVisbility(view.Gone);

执行相同操作

要在按下背面时隐藏它,请在活动

中使用此功能
 @Override
public void onBackPressed() {

       if (sheet.getVisibility() == View.VISIBLE) {
    sheet.setVisbility(view.Gone);

    }