androidandroid-recyclerviewandroid-scrollviewandroid-scrollandroid-scrolling

Two-way scrolling with RecyclerView


How do you display a list of item that is very large on one side (either horizontally / vertically), and allow it to be freely scrolled / dragged - like Google Sheets?

I've tried using a NestedScrollView with a RecyclerView, but you can only scroll in one direction at a time, and I need it to be able to be freely dragged around (no pinch / zoom functionality is required, but if it can be done it's a bonus).

Here's a GIF of what I've tried, it works but it's definitely not what I'm looking for.

enter image description here

This is the layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        layout="@layout/include_toolbar"
        android:id="@+id/toolbar" />

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_biscroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.v4.widget.NestedScrollView>

</LinearLayout>

Solution

  • I finally got an answer: Use a custom LayoutManager implementation.

    I removed the outer ScrollView, added FixedGridLayoutManager on my code, then used it as the RecyclerView's layout manager:

    FixedGridLayoutManager layoutManager = new FixedGridLayoutManager();
    layoutManager.setTotalColumnCount(adapter.getItemCount()); // This probably needs to update when item count is changed
    recyclerView.setLayoutManager(layoutManager);