显示与RecyclerView一起滚动的Viewpager

时间:2016-05-09 10:13:28

标签: android scroll android-viewpager android-recyclerview

我想显示一个Viewpager,当我放在下面的RecyclerView向上滚动时,它会向上滚动。

RecyclerView中显示的信息取决于Viewpager中显示的页面。在Viewpager上方有一个工具栏。

这是我正在使用的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout android:id="@+id/coordinatorLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".main.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/toolbarTitle"
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:gravity="center_vertical"
                android:textColor="@color/text_white"
                android:textSize="18sp"
                android:textStyle="bold"/>
        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <com.pixplicity.multiviewpager.MultiViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="220dp"
                android:background="@color/black"
                app:matchChildWidth="@+id/page"/>

            <FrameLayout
                android:id="@+id/frameLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    </ScrollView>
</android.support.design.widget.CoordinatorLayout>

注意:MultiViewPager是一个ViewPager,它显示上一页和下一页的一部分。

当选择页面时,我将framelayout替换为包含RecyclerView的Fragment。片段的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <TextView
        android:id="@+id/emptyView"
        style="@style/no_data"
        android:text="@string/movements_no_data"
        android:visibility="gone"/>
</FrameLayout>

虽然它似乎在Kitkat工作,当我在Marshmallow上尝试它时,行为很奇怪:滚动以与RecyclerView不同的方式移动Viewpager。

注2:在Kitkat中我必须强制滚动在应用程序启动时重置其位置,因为滚动最初从TOP移位。我用来重置的代码是:

    scrollView.post(new Runnable() {
        public void run() {
            scrollView.fullScroll(View.FOCUS_UP);
        }
    });

关于如何实现此布局的任何其他布局建议或想法将不胜感激。

2 个答案:

答案 0 :(得分:0)

尝试使用NestedScrollView而不是ScrollView。 话虽这么说,如果在滚动视图内使用RecyclerView,则不需要。您可以直接将应用栏滚动的行为分配给RecyclerView,它将管理appbar的滚动和折叠。 请注意,如果您使用NestedScrollView,则可能需要将其设置为填充视图端口以获得更好的性能和外观。

答案 1 :(得分:0)

我已经通过在CollapsingToolbarLayout中设置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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <com.pixplicity.multiviewpager.MultiViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:layout_marginTop="@dimen/pager_margin_top"
            app:layout_collapseMode="parallax"
            app:matchChildWidth="@+id/page"/>

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

            <TextView
                android:id="@+id/toolbarTitle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:shadowColor="#000"
                android:shadowDx="2"
                android:shadowDy="2"
                android:shadowRadius="2"
                android:textColor="#fff"
                android:textSize="18sp"
                android:textStyle="bold"/>
        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>