【问题标题】:AppBarLayout, NestedScrollView, FrameLayout, what is the deal?AppBarLayout、NestedScrollView、FrameLayout,有什么关系?
【发布时间】:2015-06-24 17:32:43
【问题描述】:

我的活动中有以下代码:

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

我想在框架布局(“main_content”id 布局)中添加一个内部带有 RecyclerView 的片段,但在这种情况下,这不起作用。

有什么问题?你知道一些例子吗?

【问题讨论】:

  • 查看本指南inthecheesefactory.com/blog/… 看看是否有帮助
  • “它不起作用”没有任何意义。
  • 你不应该在另一个也有自己滚动视图的视图中拥有一个拥有自己滚动视图的视图,但看看这个答案是否对你有帮助nested scroll view
  • 我想使用fragment将它们注入framelayout,我需要在我所有的fragment中创建AppBarLayout结构而不是在Activity布局中吗?
  • 您不应该在 NestedScrollView 中添加 RecyclerView(仅使用其中一个)。我建议使用 ViewPager 之类的东西来嵌套您的片段。这是一个例子:github.com/blackcj/DesignSupportExample

标签: android android-recyclerview


【解决方案1】:

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

<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)作为根节点。这将确保协调器布局有一个可用作目标的滚动视图。

【讨论】:

  • 你遇到的最终解决方案是什么,你能解释一下吗,我遇到了类似的情况,
  • 谢谢。你是我的救星。该解决方案应标记为正确的解决方案,它对我有用。好奇的朋友,如果不需要放任何东西,也不需要在里面创建linearLayout。
  • @blackcj 我认为框架布局没有实现 CoordinatorLayout 的布局行为
  • @fab 只要 FrameLayout 的第一个子项是 NestedScrollView 或 RecyclerView,它就可以工作。 CoordinatorLayout 能够识别正确的目标。
  • 我在框架布局中膨胀片段,,,,我的工具栏大部分时间都卡住了......有什么解决办法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-30
  • 1970-01-01
  • 2019-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多