【发布时间】: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