我正在使用CoordinatorLayout
中的观看android.support.design
。我想将app:layout_behavior
附加到片段' RecyclerView
?
在Google提供的示例中,他们只将其附加在RecyclerView
附加的同一XML文件的CoordinatorLayout
中。
有没有办法将CoordinatorLayout
附加到RecyclerView
内的片段ViewPager
?
示例位于Android开发者博客的this blog post。
答案 0 :(得分:58)
Chris Banes在Github上发布了一个示例,其中显示了您想要做的事情。
这是xml文件,它定义了如何间接地将协调器布局附加到viewpager的片段。
<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/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_done" />
</android.support.design.widget.CoordinatorLayout>
我们的想法是让viewpager拥有layout_behavior属性。
答案 1 :(得分:14)
这可能很愚蠢,但是由于构建工具没有在应用程序版本的build.gradle
中更新到22,因为它使用了21这就是为什么它不工作正如预期的那样。
编辑:
另外SanderTuit说:添加com.android.support:recyclerview-v7:22.2.0
也将解决问题
答案 2 :(得分:6)
使用FrameLayout并将片段注入到FrameLayout中。然后设置 app:layout_behavior到它。您唯一需要做的就是设置layout_behavior 到AppBayLayout的兄弟姐妹,兄弟姐妹将在工具栏下面。
<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/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
答案 3 :(得分:4)
我最近在帖子中提到了同样的问题,上述解决方案对我不起作用。幸运的是我设法解决了这个问题。所以只是分享我在这里发帖
问题是在我的代码中我设置了
recyclerView.setNestedScrollingEnabled(false);
由于尽管将layout_behaviour属性设置为viewPager,但appbar没有响应recyclerView的滚动。将上述属性更改为
recyclerView.setNestedScrollingEnabled(true);
解决了问题,appbar开始响应recylerView的滚动。
答案 4 :(得分:1)
经过几次测试后,我发现将TabLayout放在AppBarLayout之外,无论viewpager的Fragment包含什么,都可以。这是我的主要xml。
<com.tqmall.legend.konwledge.view.VerticalSwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipe_refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFE6ECF0">
<android.support.design.widget.AppBarLayout
android:id="@+id/kn_main_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="@android:color/black"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<include
layout="@layout/banner_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.9" />
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
android:background="#33000000"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.design.widget.TabLayout
android:id="@+id/main_fragment_issue_list_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:tabGravity="center"
app:tabIndicatorColor="#FF2CA2D5"
app:tabPaddingEnd="30sp"
app:tabPaddingStart="30sp"
app:tabSelectedTextColor="#FF2CA2D5"
app:tabTextColor="#FF4F5354" />
<android.support.v4.view.ViewPager
android:id="@+id/main_fragment_issue_list_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
答案 5 :(得分:-1)
我遇到了同样的问题,我解决了滚动问题,但是将工具栏和选项卡放在应用栏中,并使用coordinatorlayout包装它和viewpager。 同样在我的循环视图的布局中(要充气)我添加了layout_behavior。它有效,但问题是一切都在彼此之上。
这是我的主页
<pre id="email_test">
test@test.test
foo@bar.baz.test
foo@bar.baz.longdomain
foo-bar@foo.bar
foo_bar99@foo.bar
foo@foo@foo.bar
foo$bar#33@test.test
foo+bar-baz%99@someplace.top
</pre>
<pre id="found"></pre>
这是我的适配器布局
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/music_foot"
>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_pager"
/>
<view
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="android.support.design.widget.AppBarLayout"
android:id="@+id/appBar"
>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#1e88e5"
android:id="@+id/toolbar"
app:layout_scrollFlags="scroll|enterAlways"
></android.support.v7.widget.Toolbar>
<view
android:layout_width="match_parent"
android:layout_height="56dp"
class="com.astuetz.PagerSlidingTabStrip"
android:id="@+id/pager_tabs"
android:layout_below="@+id/appBar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</view>
如果我让它更好地生病告诉你。