Skip to content

Instantly share code, notes, and snippets.

@ThrowJojo
Created April 13, 2018 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThrowJojo/1ce15ed7eb43a74b75afd83804e6bf67 to your computer and use it in GitHub Desktop.
Save ThrowJojo/1ce15ed7eb43a74b75afd83804e6bf67 to your computer and use it in GitHub Desktop.
Example of RecyclerView + onScrollListener
class ExampleFragment : Fragment() {
var loading = false
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_example, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val layoutManager = LinearLayoutManager(context)
recyclerView.layoutManager = layoutManager
recyclerView.addOnScrollListener(object: RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val visibleItemCount = layoutManager.childCount
val totalItemCount = layoutManager.itemCount
val firstVisible = layoutManager.findFirstVisibleItemPosition()
if (!loading && (visibleItemCount + firstVisible) >= totalItemCount) {
loading = true
// Call your API to load more items
}
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment