AppBarLayout,NestedScrollView,FrameLayout,这是怎么回事? [英] AppBarLayout, NestedScrollView, FrameLayout, what is the deal?

查看:102
本文介绍了AppBarLayout,NestedScrollView,FrameLayout,这是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动中包含以下代码:

I have the following code in my activity:

    <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的片段,但是在这种情况下,这是行不通的.

I want to add a fragment with a RecyclerView inside, in the frame layout ("main_content" id layout), but in this case, this doesn't work.

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

What is the problem? do you know some example?

推荐答案

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

The collapsing toolbar should have a single scrolling target. When you replace the content, you should be replacing the scrolling container, not nesting them. For example:

<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)作为根节点.这样可以确保协调器布局具有单个滚动视图以用作目标.

Where PageRecycle has a RecyclerView (or NestedScrollView) as the root node. This will ensure the coordinator layout has a single scrolling view to use as the target.

这篇关于AppBarLayout,NestedScrollView,FrameLayout,这是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆