Android:viewpager中的recyclerview滚动问题

Android:viewpager中的recyclerview滚动问题,android,scroll,android-viewpager,android-recyclerview,material-design,Android,Scroll,Android Viewpager,Android Recyclerview,Material Design,我有一个RecyclerView在一个ViewPager中,它只占据屏幕的下半部分,如果RecyclerViews接收到垂直滚动事件,我想做的是让整个屏幕滚动 简而言之,UI层次结构: CoordinatorLayout --- AppBarLayout --- Toolbar --- Bunch of static LinearLayouts, occupy most of screen --- TabLayout --- ViewPag

我有一个
RecyclerView
在一个
ViewPager
中,它只占据屏幕的下半部分,如果
RecyclerView
s接收到垂直滚动事件,我想做的是让整个屏幕滚动

简而言之,UI层次结构:

CoordinatorLayout 
   --- AppBarLayout
       --- Toolbar
       --- Bunch of static LinearLayouts, occupy most of screen
       --- TabLayout
   --- ViewPager
       --- Fragments with RecyclerView
我拥有的

据我所知,
RecyclerView
是一种内存高效的视图,它试图将自己放在屏幕上任何可用的空间中。在我的应用程序中,我有一个
ViewPager
承载多个选项卡,每个选项卡都有一个
RecyclerView
来显示不同的内容

你能告诉我怎样才能使整个屏幕滚动吗?我猜最好的办法是添加一个<>代码> CollapsingToolbarLayout <代码>,在屏幕中间的静态内容中使用视差。我甚至尝试过这样做,但是UI完全崩溃了。这很困难,因为我只希望屏幕中间的内容滚动,而不是顶部的工具栏。我对
NestedScrollView
也没有太多的运气,显然它与
ViewPager
CoordinatorLayout
的兼容性并不直接

我想要什么:

主要活动布局:

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

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>

提前谢谢


更新:我在这里收到的回复(对此我表示感谢)来得有点晚。如果有人知道哪个是正确答案,请告诉我

我用
折叠工具栏布局
做了类似的事情

这里的技巧是有两个工具栏,一个在顶部带有searchview,另一个是专门用于折叠布局的工具栏

您的searchview工具栏将有
app:collapseMode=“pin”

您的按钮栏将显示app:collapseMode=“pin”

按钮栏下方的线性布局将具有默认折叠模式,因此将滚动。你甚至可以用
app:collapseMode=“parallax”
给它一个视差效果

您的
TabLayout
将有
app:collapseMode=“pin”
,因此它会在按钮栏下向上滚动并固定在那里

您的
ViewPager
应该位于
折叠工具栏布局
之外,并且具有属性
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”

折叠工具栏布局
将要显示标题。只需在折叠工具栏上调用
setTitle(null)
,然后在searchview工具栏上设置您的真实标题

您可以将导航和菜单放在searchview工具栏和折叠工具栏上,它们应该可以正常工作

稍后我将发布一些XML;我只是想开始回答这个问题

public class CustomRecyclerView extends RecyclerView {
public CustomRecyclerView(Context context) {
    super(context);
}

public CustomRecyclerView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomRecyclerView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

int lastEvent = -1;

boolean isLastEventIntercepted = false;
private float xDistance, yDistance, lastX, lastY;

@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            xDistance = yDistance = 0f;
            lastX = e.getX();
            lastY = e.getY();

            break;

        case MotionEvent.ACTION_MOVE:
            final float curX = e.getX();
            final float curY = e.getY();
            xDistance += Math.abs(curX - lastX);
            yDistance += Math.abs(curY - lastY);
            lastX = curX;
            lastY = curY;

            if (isLastEventIntercepted && lastEvent == MotionEvent.ACTION_MOVE) {
                return false;
            }

            if (xDistance > yDistance) {

                isLastEventIntercepted = true;
                lastEvent = MotionEvent.ACTION_MOVE;
                return false;
            }
    }

    lastEvent = e.getAction();

    isLastEventIntercepted = false;
    return super.onInterceptTouchEvent(e);
}
}


用这个改变你的回收视图

看起来您只是错过了
app:layout\u scrollFlags
属性。您应该将其添加到
工具栏
和线性标题布局中。例如,您可以使用:
app:layout\u scrollFlags=“scroll | enterally”

如我所见,您的
ViewPager
已经有了
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”
,所以它应该在您指定
滚动标志后立即工作