Skip to content

Instantly share code, notes, and snippets.

@kamikat
Last active September 1, 2021 17:40
Show Gist options
  • Save kamikat/2ba5f741f807d56a8dd7cf936571e69e to your computer and use it in GitHub Desktop.
Save kamikat/2ba5f741f807d56a8dd7cf936571e69e to your computer and use it in GitHub Desktop.
NestedScrollView + SwipeRefreshLayout + RecyclerView
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusableInTouchMode="true"> <!-- Prevents scrolling location from being overriden by RecyclerView -->
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center"
android:text="Contents above RV"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
@kamikat
Copy link
Author

kamikat commented Aug 26, 2016

Checklist

  • recyclerView.setNestedScrollingEnabled(false) for smooth scrolling/fling
  • layoutManager.setAutoMeasureEnabled(true) if RecyclerView does not render items.

@twisstosin
Copy link

twisstosin commented May 22, 2017

Mine seems to take a lotta time to render recyclerView items, and sometimes doesn't render at all.

@satputekuldip
Copy link

satputekuldip commented Jul 25, 2017

`

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/search_edit_frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <android.support.v7.widget.SearchView
            android:id="@+id/product_list_search_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:fadingEdgeLength="3dp"
            android:focusableInTouchMode="true" />

        <ImageButton
            android:id="@+id/btn_purchase_barcode_scanner"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:background="@color/white"
            android:src="@mipmap/ic_barcode" />
    </LinearLayout>


    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/search_edit_frame"
        android:fillViewport="true">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/add_to_cart_swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.widget.NestedScrollView
                android:id="@+id/add_to_cart_product_nested_scroll_View"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:focusableInTouchMode="true">

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

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


    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>
`

RecyclerView is not displaying all items

@sam453
Copy link

sam453 commented Jul 25, 2017

pull-up refresh invalid ....

@michale421
Copy link

gjkhgkj

@RahulSDeshpande
Copy link

RahulSDeshpande commented Dec 22, 2017

@sam453 Yes, exactly.

Reason:
As NextedScrollView won't let recycle the items of RecyclerView & loads all the items in one shot, pull-up refresh won't work.

Workaround:
We need to manually/listen-to monitor the NestedScrollView's scrolling event & do the load-more-items thing in child's RecyclerView.
i.e., as soon the NestedScrollView scrolls to its last, callback the RecyclerView's adapter & load items.

👍
Cheers !!

@danlero
Copy link

danlero commented Jan 16, 2018

Using same layout and scrolling is working but it's not smooth.

@erlangparasu
Copy link

i think recyclerview height must be match_parent

@fukemy
Copy link

fukemy commented Sep 1, 2021

i have horizontal and vertical scroll view and got many issues:

<androidx.core.widget.NestedScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:fillViewport="true"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusableInTouchMode="true"
            android:orientation="vertical">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rcStory"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:nestedScrollingEnabled="false"
                android:background="@color/red" />

            <com.jcodecraeer.xrecyclerview.XRecyclerView
                android:id="@+id/rcRecent"
                android:layout_width="match_parent"
                android:descendantFocusability="blocksDescendants"
                android:layout_weight="1"
                android:nestedScrollingEnabled="false"
                android:layout_height="match_parent"
                android:background="@color/mainGray" />
        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment