Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Endless load more recyclerview inside nested scroll android

I have Recyclerview inside the nestedscrollview and I want to scroll the nestedscrollview to the buttom display loading and load more list to recyclerview.

Here is my xml.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="kh.com.iknow.endless.MainActivity">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Filter"/>
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Sort"/>
            </LinearLayout>
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"
                android:scrollbars="vertical" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>
like image 990
Angkor Empire Avatar asked Dec 06 '16 07:12

Angkor Empire


Video Answer


1 Answers

you can do it in two ways :

1- adding header to recyclerview

2- using setOnScrollChangeListener method of NestedScrolview

I prefer the second way :

 NestedScrollView nestedSV = (NestedScrollView)      findViewById(R.id.nested_sync_scrollview);

 if (nestedSV != null) {

        nestedSV.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                String TAG = "nested_sync";
                if (scrollY > oldScrollY) {
                    Log.i(TAG, "Scroll DOWN");
                }
                if (scrollY < oldScrollY) {
                    Log.i(TAG, "Scroll UP");
                }

                if (scrollY == 0) {
                    Log.i(TAG, "TOP SCROLL");
                }

                if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
                    Log.i(TAG, "BOTTOM SCROLL");
                    if (!isRecyclerViewWaitingtoLaadData) //check for scroll down
                    {

                        if (!loadedAllItems) {
                            showUnSentData();
                        }
                    }
                }
            }
        });
    }
like image 144
iDeveloper Avatar answered Oct 02 '22 05:10

iDeveloper