【问题标题】:CoordinatorLayout + Fragment + NavigationDrawerCoordinatorLayout + Fragment + NavigationDrawer
【发布时间】:2016-05-23 22:41:16
【问题描述】:

我正在尝试将 CoordinatorLayout 实现到我的应用程序中。 我尝试了许多不同的教程,还尝试针对我的问题调整 CoordinatorLayout + AppBarLayout + NavigationDrawer 上的解决方案,但我不知道为什么它不起作用。

<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/id_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/expandedImage"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@drawable/header"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.7" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_actionbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            android:minHeight="@dimen/abc_action_bar_default_height_material"
            app:layout_scrollFlags="scroll|enterAlways" />
    </android.support.design.widget.CollapsingToolbarLayout>

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

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true">

        <Button
            android:id="@+id/emiter"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center_horizontal|top"
            android:layout_marginTop="-70dp"
            android:text="" />
    </FrameLayout>

    <fragment
        android:id="@+id/fragment_drawer"
        android:name="com.myapp.test.navigationdrawer.NavigationDrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>

协调器本身似乎正在工作,因为我可以获得效果,但只有在使用工具栏“滚动”屏幕上部时。它不适用于工具栏下方片段中的 ScrollView。 我做错了什么?

【问题讨论】:

  • 什么不起作用?您谈论的 ScrollView/Fragment 不是问题中代码的一部分?
  • @jayeshsolanki93 是的,所以片段是不同的,我用 fragmentTransaction.replace(R.id.container, newFragment);

标签: android android-fragments android-coordinatorlayout navigation-drawer android-nestedscrollview


【解决方案1】:

它不适用于 Fragment 中的 ScrollView

CoordinatorLayout 只能监听嵌套滚动。使用NestedScrollViewRecyclerView 作为您将添加到Activity 中的每个片段的根视图,并且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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/id_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/expandedImage"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@drawable/test"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.7" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_actionbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            android:minHeight="@dimen/abc_action_bar_default_height_material"
            app:layout_scrollFlags="scroll|enterAlways" />
    </android.support.design.widget.CollapsingToolbarLayout>

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

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true">

        <Button
            android:id="@+id/emiter"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center_horizontal|top"
            android:layout_marginTop="-70dp"
            android:text="" />
    </FrameLayout>

    <fragment
        android:id="@+id/fragment_drawer"
        class="ru.solodovnikov.test.MainFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>

您的 fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:text="TOOOOOO LONG TEXT..."
        android:layout_height="wrap_content" />
</android.support.v4.widget.NestedScrollView>

【讨论】:

  • 所以我根本不能将它用于片段?
  • 你可以使用ScrollView。但是 CoordinatorLayout 不会接收滚动事件,并且您在其中的视图不会对滚动做出反应。所以只需将您的 ScrollView 更改为 NestedScrollView。
  • 所以但基本上我不能在我的 acitivity_main.xml 文件中使用这个代码(所以我只需要它一次)而是我必须将它添加到每个 Fragment 的布局 xml 文件中?
  • 请看我编辑的答案。是的,您需要在每个片段的布局 xml 文件中使用 NestedScrollView 作为根视图。
  • 非常感谢!这完美!我不知道每个片段中都需要一个 NestedScrollView :)
猜你喜欢
  • 2015-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多