Android:在viewpager中滚动使用recyclerview的问题

not*_*ain 5 android scroll android-viewpager material-design android-recyclerview

我有一个只占据屏幕下半部分的RecyclerView内部ViewPager,我想要做的是如果RecyclerViews接收到垂直滚动事件则整个屏幕滚动.

UI层次结构简而言之:

CoordinatorLayout 
   --- AppBarLayout
       --- Toolbar
       --- Bunch of static LinearLayouts, occupy most of screen
       --- TabLayout
   --- ViewPager
       --- Fragments with RecyclerView
Run Code Online (Sandbox Code Playgroud)

我有什么: 是)我有的

根据我的理解,a RecyclerView具有内存效率,并试图适应屏幕中可用的任何空间.在我的应用程序中,我有一个ViewPager托管多个选项卡,每个选项卡都有一个RecyclerView显示不同的东西.

你能否告诉我一些关于如何使整个屏幕滚动的想法?我猜最好的镜头是CollapsingToolbarLayout在屏幕中间添加一个带有视差的静态内容.我甚至试图这样做,但UI完全被打破了.这很困难因为我只希望屏幕中间的内容滚动出来,而不是顶部的工具栏.我没有带多少运气NestedScrollView或者,显然它的兼容性ViewPagerCoordinatorLayout不直进..

我想要的是: 我想要的是

主要活动布局:

<android.support.design.widget.CoordinatorLayout
    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.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_contributor"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:focusableInTouchMode="true">

            <android.support.v7.widget.SearchView
                android:id="@+id/searchview_contributor"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:icon="@drawable/ic_search_white_24dp"
                app:defaultQueryHint="Bla bla"
                app:iconifiedByDefault="false"/>

            <ImageButton
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:background="@android:color/transparent"
               android:src="@drawable/ic_person_outline_black_24dp"/>

        </android.support.v7.widget.Toolbar>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:text="La Bla"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:text="Bla Bla"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            >

            <!-- Bunch of stuff, but irrelevant -->

            <android.support.design.widget.TabLayout
                android:id="@+id/tablayout_profile"
                android:layout_width="match_parent"
                android:layout_height="40dp"/>
        </LinearLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager_profile"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

Viewpager中的片段:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview_photos"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

提前致谢!

更新:我在这里收到的回复(感谢我的回复)来得太晚了.如果有人知道哪个是正确的答案,请告诉我!

kri*_*son 2

我已经用 a 做了一些与此非常相似的事情CollapsingToolbarLayout

这里的技巧是有两个工具栏,一个位于顶部的搜索视图,另一个是专用于折叠布局的工具栏。

您的搜索视图工具栏将具有app:collapseMode="pin".

你的按钮栏将会有app:collapseMode="pin".

按钮栏下方的 LinearLayout 将具有默认折叠模式,因此它会滚动。您甚至可以使用 为其赋予视差效果app:collapseMode="parallax"

TabLayout将拥有app:collapseMode="pin",因此它会在按钮栏下方向上滚动并固定在那里。

你的ViewPager应该在 之外CollapsingToolbarLayout并且具有属性app:layout_behavior="@string/appbar_scrolling_view_behavior"

将会CollapsingToolbarLayout想要显示一个标题。只需调用setTitle(null)折叠工具栏并在搜索视图工具栏上设置您的真实标题即可。

您可以将导航和菜单放在搜索视图工具栏和折叠工具栏上,它们应该按预期工作。

稍后我会发布一些 XML;我只是想开始这个答案。