Уведомление о конце скролинга RecyclerView
- import android.app.Activity;
- import android.support.v7.widget.RecyclerView;
- public class LoadingListHelper {
- private Activity activity;
- private RecyclerView.Adapter adapter;
- private RecyclerView.LayoutManager layoutManager;
- private LoadingListener loadingListener;
- public LoadingListHelper(Activity activity, RecyclerView recyclerView) {
- this.activity = activity;
- adapter = recyclerView.getAdapter();
- layoutManager = recyclerView.getLayoutManager();
- recyclerView.addOnScrollListener(onScrollListener);
- }
- public void setScrollEndListener(LoadingListener loadingListener) {
- this.loadingListener = loadingListener;
- }
- private RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
- @Override
- public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
- super.onScrolled(recyclerView, dx, dy);
- if (layoutManager.findViewByPosition(adapter.getItemCount() - 1) != null) {
- if (loadingListener != null) {
- loadingListener.onScrolledToEnd();
- }
- }
- }
- };
- public interface LoadingListener {
- void onScrolledToEnd();
- }
- }
Может пригодится для реализации подгружаемых списков. Пример.
- new LoadingListHelper(getActivity(), mRecyclerView)
- .setScrollEndListener(new LoadingListHelper.LoadingListener() {
- @Override
- public void onScrolledToEnd() {
- // TODO: load new data or something else
- }
- });