使用XML中的多个RecyclerView滚动问题

时间:2017-03-17 10:27:07

标签: android xml android-recyclerview scrollview

在xml中使用multiple RecyclerView ,前两个RecyclerViews(水平)和第三个RecyclerView(垂直)

正如您在下面的屏幕截图中看到的那样,每当我滚动滚动时,它只会滚动第三个RecyclerView而不是我在这个上面使用的RecyclerView

然而,我想滚动所有这些RecyclerView,如果我滚动(向上滚动或向下滚动)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="8dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/horizontal_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:scrollbars="none" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/horizontal_rv"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:scrollbars="none" />

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

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

尝试通过以下方式扩展listview以支持自定义滚动:

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import java.lang.reflect.Field;

public class ParallaxRecyclerView extends RecyclerView {


    private State state;
    private Recycler recycler;

    public ParallaxRecyclerView(Context context) {
        this(context,null);
    }

    public ParallaxRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ParallaxRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        Field fieldState = null;
        Field fieldRecycler = null;
        try {
            fieldState = this.getClass().getSuperclass().getDeclaredField("mState");
            fieldState.setAccessible(true);
            state = (State) fieldState.get(this);

            fieldRecycler = this.getClass().getSuperclass().getDeclaredField("mRecycler");
            fieldRecycler.setAccessible(true);
            recycler = (Recycler)fieldRecycler.get(this);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    public void scrollHorizontallyBy(int dx){
        getLayoutManager().scrollHorizontallyBy(dx,recycler,state);
    }

    public void scrollVerticallyBy(int dy){
        getLayoutManager().scrollVerticallyBy(dy,recycler,state);
    }
}

然后将滚动列表内容添加到顶部的recyclerview并将滚动增量值传递给

scrollHorizontallyBy

例如:

topRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
...
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
parallaxRecyclerView.scrollHorizontallyBy(dx);
parallaxRecyclerView.scrollVerticallyBy(dy);
}
...
})

答案 1 :(得分:0)

我找到了最好最简单的解决方案,我只是复制了 ScrollView 中的所有内容,并使用https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={your access_token} 作为 wrap_content 用于第三个​​和垂直 RecyclerView

height

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:fillViewport="true"> ...... </ScrollView> 文件中的的更新版本:

gradle
相关问题