1

Scrolling problem with multiple RecyclerViews in an XML

 2 years ago
source link: https://www.codesd.com/item/scrolling-problem-with-multiple-recyclerviews-in-an-xml.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Scrolling problem with multiple RecyclerViews in an XML

advertisements

Using multiple RecyclerView in an xml, first two RecyclerViews (Horizontal) and third RecyclerView (Vertical)

As you can see in below screenshot, Whenever, I do Scrolling it scrolls third RecyclerView only not both the RecyclerView(s) those I am using on top of that one

Whereas, I want to scroll all these RecyclerView, If I do scrolling (Scroll Up or Scroll Down)

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


Try this to extend listview to support custom scrolling by :

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);
    }
}

Then add a scroll listnener to your top recyclerview and pass the scroll delta value to

scrollHorizontallyBy

For example:

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);
}
...
})




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK