Karp
Karp

Reputation: 41

Two Vertical RecyclerView scrolling at the same time

I have two RecyclerView, both have a vertical orientation, I need to scroll one of them so that the second scrolls, that is, their scrolling is synchronous, I thought that it is possible to apply one LinearLayoutManager to these two RecyclerView and then it will be work, but in this log, the error LinearLayoutManager is already attached to a RecyclerView will be generated, so I don't know how to be, help me find a solution, I need two independent RecyclerView with different adapters, but which scroll synchronously, so do not write about GridLayoutManager, thanks.

xml

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_0"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:clipToPadding="false"
            android:orientation="vertical"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:clipToPadding="false"
            android:orientation="vertical"/>
    </LinearLayout>

Cod

                val RLM_0 = LinearLayoutManager(context)
                rv_0.setHasFixedSize(false)
                rv_0.isNestedScrollingEnabled = false
                rv_0.layoutManager = RLM_0
                adapter_0 = Adapter_0(itemTasks, requireActivity())
                rv_0.adapter = adapter_0


                val RLM_1 = LinearLayoutManager(context)
                rv_1.setHasFixedSize(false)
                rv_1.isNestedScrollingEnabled = false
                rv_1.layoutManager = RLM_1
                adapter_1 = Adapter_1(itemTasks, requireActivity())
                rv_1.adapter = adapter_1

Upvotes: 1

Views: 1617

Answers (1)

Drioueche Mohammed
Drioueche Mohammed

Reputation: 560

Solution 1:

Check is: Sync scrolling of multiple RecyclerViews

Solution 2:

There is an other way to do so, the idea is to disable scroll for each recycler view and surround them with a scroll view.

This is how you can implement it:

binding.recyclerViewOne.setOnTouchListener((v, event) -> true);
binding.recyclerViewTwo.setOnTouchListener((v, event) -> true);

And for the layout:

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
    </LinearLayout>
</ScrollView>

Upvotes: 2

Related Questions