Android - CoordinatorLayout 中的滚动和 Bottom Sheet

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

首先,我不熟悉 CoordinatorLayouts,我有一个包含多个可滚动 CardView 的 fragment ,当我尝试添加 Bottom Sheet 时,我得到了 Bottom Sheet must be a child of a coordinatorLayout错误,所以我添加了一个 coordinatorLayout,现在我似乎无法再滚动了

PS:我尝试用 NestedScrollView 替换 ScrollView 但它没有用,我什至尝试编辑 XML 标签顺序但是什么都没有解决,我仍然无法滚动

有没有一种方法可以在不使用 CoordinatorLayout 的情况下使用 Bottom Sheets ??

这是带有 ScrollView 的 XML 布局 fragment :

<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 布局 fragment :

<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>

主要 Activity 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>

最佳答案

编辑:从 fragment 中移除 BottomSheetLayout,并将其放入 ViewPager 下的 Activity 中。然后将一个 NestedScrollView 放在 fragment 的顶部,具有如下属性:

<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 仅出现在 fragment 中,您可以这样做: 将可见性设置为 android:visiblity="gone",并在 fragment 中将其设置为可见,如下所示:

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

然后,在您拥有的其他 fragment 中,执行相同的操作,只需使用 sheet.setVisbility(view.Gone);

要在按下返回时隐藏它,请在 Activity 中使用它:

 @Override
public void onBackPressed() {

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

    }

关于Android - CoordinatorLayout 中的滚动和 Bottom Sheet ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39059523/

相关文章:

Android 设计布局 ListItem RecyclerView Horizo​​ntal

java - Android 联系人使用 ContactsSource 来实现较低的 api 级别,并使用 ContactsAccountType 作为 contacts.xml 中的默认值

Java DOM 解析器 XML

android - 从另一个 Activity 刷新 RecyclerView 后添加一个值

android - 滚动 |始终输入折叠系统任务栏,而不是我的工具栏

android - 如何从动态 ArrayList 为 Android SQLite 查询编写 WHERE IN 子句

java - "Error initializing Network Connection: Class not found",源: file:///android_asset/www/cordova.js

java - 当我使用sqlite运行android程序时如何防止重复行

android - FragmentManager(v4) 不从 mCreatedMenus 中删除 fragment

android - 在 Android webview 中保存图像的上下文菜单