Created
April 13, 2018 01:27
-
-
Save ThrowJojo/1ce15ed7eb43a74b75afd83804e6bf67 to your computer and use it in GitHub Desktop.
Example of RecyclerView + onScrollListener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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