带有FrameLayout容器的AppBarLayout作为滚动内容不起作用

我试图使用最新的devise库,使我的工具栏隐藏/滚动显示。 我的问题是滚动的内容,我在片段,我只是注入到FrameLayout容器,它不起作用。 这是我的活动:

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.CoordinatorLayout android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <include layout="@layout/layout_toolbar" /> </android.support.design.widget.AppBarLayout> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout> <FrameLayout android:id="@+id/navigation_drawer_container" android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent" android:layout_gravity="start" tools:layout="@layout/fragment_nav_drawer_anon" /> 

和我的片段:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/pull_to_refresh" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.v4.widget.SwipeRefreshLayout> <TextView android:id="@android:id/empty" android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="18sp" android:fontFamily="sans-serif" android:color="@color/dark_grey" android:padding="60dp"/> 

和工具栏:

 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways" style="@style/Widget.MyApp.ActionBar" /> 

我正在关注官方文档和这个演示 ,但仍然无法弄清楚如何使其工作。

android.support.v4.widget.NestedScrollViewreplace你的FrameLayout

NestedScrollView就像ScrollView一样,但是它支持在Android的新版本和旧版本中同时作为嵌套的滚动父代和子代。 嵌套滚动默认启用。

链接到文档

这种行为的原因是Framelayout没有指定行为。 CoordinatorLayout依赖子视图来处理行为。

你可以在这里阅读

http://android-developers.blogspot.in/2015/05/android-design-support-library.html

它指出

CoordinatorLayout和自定义视图

需要注意的一件事是,CoordinatorLayout对FloatingActionButton或AppBarLayout工作没有任何天生的理解 – 它只是以Coordinator.Behavior的forms提供了一个额外的API,它允许子视图更好地控制触摸事件和手势以及声明彼此之间的依赖关系,并通过onDependentViewChanged()接收callback。

视图可以通过使用CoordinatorLayout.DefaultBehavior(YourView.Behavior.class)注释来声明一个默认行为,或者使用app:layout_behavior =“com.example.app.YourView $ Behavior”属性在布局文件中设置它。 这个框架使得任何视图都可以与CoordinatorLayout集成。

编辑:尽pipeFrameLayout不是一个自定义视图,它没有指定CoordinateLayout寻求的行为。

使用FrameLayout作为CoordinatorLayout的子项很好。 工具栏正在崩溃,就像它应该。 当我使用过时的库时,我在开始时遇到了问题。

下面是我现在使用的gradle依赖关系:

 compile 'com.android.support:appcompat-v7:22.2.1' compile 'com.android.support:cardview-v7:22.2.0' compile 'com.android.support:recyclerview-v7:22.2.0' compile 'com.android.support:design:22.2.0' 

我将FrameLayout的属性app:layout_behavior="@string/appbar_scrolling_view_behavior"作为CoordinatorLayout一个子项放置在一个活动的布局中。 FrameLayout作为碎片的容器。 我的片段布局的根元素是一个RecyclerView或一个NestedScrollView

这里是活动的布局:

  <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent" > <FrameLayout android:id="@+id/..." android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <android.support.design.widget.AppBarLayout android:layout_height="192dp" android:layout_width="match_parent" > <android.support.design.widget.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed" > <ImageView android:id="@+id/.." android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:scaleType="centerCrop" android:src="@drawable/..." app:layout_collapseMode="parallax"/> <android.support.v7.widget.Toolbar android:id="@+id/toolbar_sessions" android:layout_height="?attr/actionBarSize" android:layout_width="match_parent" app:layout_collapseMode="pin" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout> 

我的第一个片段的布局如下所示:

 <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="..." /> 

我的第二个片段的布局如下所示:

 <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="..." android:layout_width="match_parent" android:layout_height="match_parent" tools:context="..." > ... </android.support.v4.widget.NestedScrollView> 

在我的应用程序,它只适用于RecyclerView。 也许你应该使用RecyclerView而不是ListView。

您可以在CoordinatorLayout中使用Framelayout进行滚动。 为此,您必须在app:layout_behavior="@string/appbar_scrolling_view_behavior"使用app:layout_behavior="@string/appbar_scrolling_view_behavior"以及您希望使用此折叠效果的滚动视图。

对于前者,如果你在你的frameLayout里面joinRecyclerView,那么你必须在你的recyclerrView里面使用app:layout_behavior="@string/appbar_scrolling_view_behavior"

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </Relativelayout> 

要么,

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" android:orientation="vertical"> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_above="@+id/bNext" android:fillViewport="true" android:scrollbars="none" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-- your content --> </android.support.v4.widget.NestedScrollView> </Relativelayout> 

因此,使用这个过程,你可以使用frameLayout实现滚动,无论它是否包含回收器或嵌套滚动视图。

这是一个很好的问题。 我也有同样的。

您的容器FrameLayout被定义正确的问题在于Fragment 。 该Fragment应该有RecyclerView而不是ListView因为它现在已经被弃用了。

例:

 <android.support.v7.widget.RecyclerView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="false" android:paddingTop="@dimen/activity_vertical_margin"/> 

我在我的应用程序中使用相同的结构,它的作品完美。

ListView不执行NestedScrollingChild所以它不起作用。

RecyclerView,所以它可以传播滚动到NestedScrollingParent(CoordinatorLayout)。

你只需要更换

的FrameLayout

android.support.v4.widget.NestedScrollView

通过这种方式:

 <android.support.design.widget.CoordinatorLayout android:id="@+id/root_coordinator" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar_layout" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways"> <ImageView android:layout_width="match_parent" android:layout_height="192dp" android:scaleType="centerCrop" android:src="@drawable/header" app:layout_collapseMode="parallax" /> <android.support.v7.widget.Toolbar android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:layout_collapseMode="pin" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <!-- This was before FrameLayout --> <android.support.v4.widget.NestedScrollView android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> </android.support.v4.widget.NestedScrollView> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right" android:layout_margin="16dp" android:src="@drawable/ic_drawer_alertas" app:borderWidth="0dp" app:fabSize="mini" /> </android.support.design.widget.CoordinatorLayout> <android.support.design.widget.NavigationView android:id="@+id/navigation_drawer" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/drawer_header" app:itemIconTint="@color/colorAccent" app:itemTextColor="@color/colorSecondaryText" app:menu="@menu/menu_main" /> 

您必须将行为添加到滚动视图:

 <android.support.v4.widget.SwipeRefreshLayout android:layout_marginTop="5dp" android:id="@+id/swipe_main" android:enabled="false" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <com.marshalchen.ultimaterecyclerview.UltimateRecyclerView android:id="@+id/rvUserProfile" app:recyclerviewEmptyView ="@layout/ev_home" app:recyclerviewClipToPadding="false" android:layout_width="match_parent" android:layout_height="match_parent"></com.marshalchen.ultimaterecyclerview.UltimateRecyclerView> </android.support.v4.widget.SwipeRefreshLayout> 

app:layout_scrollFlags="scroll|enterAlways"从工具栏移动到Framelayout。 对不起,来晚了。