Created
February 21, 2016 21:44
-
-
Save mtsahakis/abde764a54eba007483a to your computer and use it in GitHub Desktop.
Check if an Android Recycler View is scrolled all the way down. Implementation is taken from a Fragment.
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
private RecyclerView mRecyclerView; | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_view, container, false); | |
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); | |
// ommiting other recycler view set up, such as adapter and Layout manager set up .. | |
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { | |
@Override | |
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { | |
super.onScrolled(recyclerView, dx, dy); | |
if (isLastItemDisplaying()) { | |
// do something cool like add more items | |
} | |
} | |
}); | |
return view; | |
} | |
private boolean isLastItemDisplaying() { | |
RecyclerView.Adapter adapter = mRecyclerView.getAdapter(); | |
if (adapter != null && adapter.getItemCount() != 0) { | |
int lastVisibleItemPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition(); | |
if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == adapter.getItemCount() - 1) { | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment