Android 6.0 ScrollView and RecyclerView Sliding Conflict

Keywords: Android

Preface: I have a requirement that Vertical show two different recycleviews in a Linear Layout, but require both recycleviews to be displayed.

In Android 5.0, my approach is to add a ScrollView outside Linear Layout to solve my problem. Running on Android 5.0 machine, the effect is really what I want. The code is as follows:

    <ScrollView
        android:id="@+id/sv_search_result"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/size_10"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
                <com.hp.appquestion.view.CustomRecyclerView
                    android:id="@+id/rv_video_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <com.hp.appquestion.view.CustomRecyclerView
                    android:id="@+id/rv_exam_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>

But when this code runs on the Android 6.0 machine, I find that the content of the second Recycler View is not displayed, and the second Recycler View can slide. There is also a problem, even if only one Recycler View is placed in ScrollView, and all of them are displayed, there is a clear sense of Carton when sliding.

Looking up the information on the internet, it is said that in Android 6.0 system, if RecyclerView and RecyclerView are used together, there really exists the problem of sliding conflict between them. Sliding conflicts need to be solved by oneself.

Combined with the information on the Internet, my own solution is.

1. Firstly, solve the problem that the first recycler view can be displayed completely.

Outside the two recyclerviews are wrapped a relative layout Relative Layout, which must be wrapped for both recyclerviews. I tried to wrap only one at the bottom and found that when the data at the bottom increased, there would be an exception.

The code is as follows:

    <ScrollView
        android:id="@+id/sv_search_result"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/size_10"
        android:scrollbars="none">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.hp.appquestion.view.CustomRecyclerView
                    android:id="@+id/rv_video_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.hp.appquestion.view.CustomRecyclerView
                    android:id="@+id/rv_exam_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>

This solves the problem that both recycler views on Android 6.0 can be fully displayed.

2. Solve the problem of Karton in sliding recyclerView

My solution is to rewrite the Linear Layout Manager, set it to be non-slidable, and rely on ScrollView for external sliding, which solves the problem of Carton when sliding.
The code is as follows:

    public class ScrollLinearLayoutManager extends LinearLayoutManager {
        private boolean isScrollEnabled = true;

        public ScrollLinearLayoutManager(Context context) {
            super(context);
        }

        public ScrollLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }

        public ScrollLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        public void setScrollEnabled(boolean flag) {
            this.isScrollEnabled = flag;
        }

        @Override
        public boolean canScrollVertically() {
            return isScrollEnabled && super.canScrollVertically();
        }
    }

Use:

    ScrollLinearLayoutManager scrollLinearLayoutManager = new ScrollLinearLayoutManager(this);
    scrollLinearLayoutManager.setScrollEnabled(false);
    mRecyclerView.setLayoutManager(scrollLinearLayoutManager);

In this way, both problems have been solved!

Posted by cedtech31 on Sun, 07 Jul 2019 11:12:29 -0700