AppBarLayout,NestedScrollView,FrameLayout,这是怎么一回事?

9

我在我的活动中有以下代码:

    <android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="160dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <FrameLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
    </android.support.v4.widget.NestedScrollView>

我想在帧布局(id为“main_content”的布局)中添加一个包含RecyclerView的碎片,但在这种情况下,这样做行不通。
问题出在哪里?你知道一些例子吗?

3
请查看这个指南 http://inthecheesefactory.com/blog/android-design-support-library-codelab/en 并查看它是否有帮助。 - EpicPandaForce
2
“它不工作” 意味着什么都没有。 - Marcin Orlowski
你不应该在一个拥有自己滚动视图的视图内部再放置另一个拥有自己滚动视图的视图,但是可以参考这个答案nested scroll view - Jhonatas
我想使用fragment将它们注入到FrameLayout中,那么我需要在所有的fragment中创建AppBarLayout结构,而不是在Activity布局中吗? - fab
不应该在NestedScrollView中添加RecyclerView(只使用其中之一)。我建议使用ViewPager来嵌套您的Fragments。这是一个例子:https://github.com/blackcj/DesignSupportExample - blackcj
@blackcj 如果不想使用ViewPager,只想使用FrameLayout来注入不同的带有RecyclerView的片段或不带RecyclerView的片段,那么在这种情况下就没有解决方案吗? - fab
1个回答

11

折叠工具栏应该有一个单一的滚动目标。当您替换内容时,应该替换滚动容器,而不是嵌套它们。例如:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_container"
        android:background="@color/colorPrimary"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <!-- Your content here -->
            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>
    </FrameLayout>

    <include
        layout="@layout/toolbar"/>

</android.support.design.widget.CoordinatorLayout>
你可以随后调用:

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

PageRecycle recycle = PageRecycle.create();
ft.replace(R.id.main_container, recycle);
ft.commit();

在PageRecycle中,根节点应该是RecyclerView(或NestedScrollView)。这将确保协调布局有一个单一滚动视图作为目标。


1
你最终找到了什么解决方案?能否给我一些提示?我也遇到了类似的情况,卡住了。 - Srishti Roy
2
谢谢。你是我的救星。这个解决方案应该被标记为正确的解决方案,它对我有用。对于那些好奇的人,如果您不需要在其中放置任何内容,则无需在if语句内创建linearLayout。 - Simon
@blackcj,框架布局没有实现CoordinatorLayout的布局行为,我认为。 - fab
@fab 只要 FrameLayout 的第一个子项是 NestedScrollView 或 RecyclerView,这个方法就能正常工作。CoordinatorLayout 能够正确识别目标。 - blackcj
我正在FrameLayout中填充Fragment,我的工具栏大多数时候都会卡住...有什么解决办法吗? - Rishabh Srivastava
太好了!!!感谢您,经过数小时的搜索,这就是替换不同类型片段的解决方案。 - Luchi Valles

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接