Skip to content

Instantly share code, notes, and snippets.

@karntrehan
Created March 4, 2019 11:03
Show Gist options
  • Save karntrehan/bda4d5e693055000bf1a76cf3e15174d to your computer and use it in GitHub Desktop.
Save karntrehan/bda4d5e693055000bf1a76cf3e15174d to your computer and use it in GitHub Desktop.
NestedScrollView + Paginated Recyclerview Android
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.fragment_data.nsvData
import kotlinx.android.synthetic.main.fragment_data.rvData
class DataFragment : Fragment() {
override fun onViewCreated(
view: View,
savedInstanceState: Bundle?
) {
super.onViewCreated(view, savedInstanceState)
enablePagination(
nestedScrollView = nsvData,
recyclerView = rvData,
nextPageFunction = viewModel::fetchNextData
)
}
private var paginatedRV: RecyclerView? = null
private fun enablePagination(
nestedScrollView: NestedScrollView,
recyclerView: RecyclerView,
nextPageFunction: () -> Unit
) {
//Disable nested recyclerview from scrolling
ViewCompat.setNestedScrollingEnabled(recyclerView, false)
//Attach scroll listener to nested scrollview
nestedScrollView.viewTreeObserver?.addOnScrollChangedListener {
//If the paginated rv is not calculated already
if (paginatedRV == null) {
//Get the parent holder
val holder = nestedScrollView.getChildAt(0) as ViewGroup
//Loop through all children of parent holder
for (i in 0 until holder.childCount) {
//Pull the pagination recyclerview child
if (holder.getChildAt(i).id == recyclerView.id) {
paginatedRV = holder.getChildAt(i) as RecyclerView
break
}
}
}
paginatedRV?.let {
//Identify if recyclerview is scrolled to bottom
if (it.bottom - (nestedScrollView.height + nestedScrollView.scrollY) == 0)
nextPageFunction()
}
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
android:orientation="vertical"
tools:context="com.karntrehan.DataFragment"
>
<include layout="@layout/toolbar"/>
<androidx.core.widget.NestedScrollView
android:id="@+id/nsvData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
tools:visibility="visible"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="@+id/btnViewed"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_gravity="end|right"
android:layout_marginTop="16dp"
android:background="@color/white"
android:padding="8dp"
android:text="@string/viewed"
android:textColor="@color/recently_viewed_text_color"
android:textSize="12sp"
style="@style/Button.Flat"
/>
<TextView
android:id="@+id/tvDataCount"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:textColor="@color/text_color_primary"
tools:text="Showing 248 Events"
style="@style/Text.Title.Regular"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="3"
tools:listitem="@layout/item_data"
/>
<ProgressBar
android:id="@+id/pbLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
style="?android:attr/progressBarStyle"
/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<include
layout="@layout/layout_empty"
android:id="@+id/noData"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="gone"
tools:visibility="gone"
/>
</LinearLayout>
@raviBagga
Copy link

please help in explaining nextPageFunction = viewModel::fetchNextData this view model function

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