【问题标题】:Display a Viewpager that scrolls together with a RecyclerView显示与 RecyclerView 一起滚动的 Viewpager
【发布时间】:2016-05-09 10:13:27
【问题描述】:

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

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 上尝试它时,行为很奇怪:滚动移动 Viewpager 的方式与 RecyclerView 不同。

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

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

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

【问题讨论】:

  • 将所需布局作为标题添加到您的 recyclerView
  • 按建议编辑。

标签: android scroll android-viewpager android-recyclerview


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我通过在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多