Is there a callback for when RecyclerView has finished showing its items after I've set it with an adapter?
I've found a way to solve this (thanks to user pskink), by using the callback of LayoutManager
:
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false) {
@Override
public void onLayoutChildren(final Recycler recycler, final State state) {
super.onLayoutChildren(recycler, state);
//TODO if the items are filtered, considered hiding the fast scroller here
final int firstVisibleItemPosition = findFirstVisibleItemPosition();
if (firstVisibleItemPosition != 0) {
// this avoids trying to handle un-needed calls
if (firstVisibleItemPosition == -1)
//not initialized, or no items shown, so hide fast-scroller
mFastScroller.setVisibility(View.GONE);
return;
}
final int lastVisibleItemPosition = findLastVisibleItemPosition();
int itemsShown = lastVisibleItemPosition - firstVisibleItemPosition + 1;
//if all items are shown, hide the fast-scroller
mFastScroller.setVisibility(mAdapter.getItemCount() > itemsShown ? View.VISIBLE : View.GONE);
}
};
The good thing here is that it works well and will handle even keyboard being shown/hidden.
The bad thing is that it gets called on cases that aren't interesting (meaning it has false positives), but it's not as often as scrolling events, so it's good enough for me.
EDIT: there is a better callback that was added later, which doesn't get called multiple times. Here's the new code instead of what I wrote above:
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false) {
@Override
public void onLayoutCompleted(final State state) {
super.onLayoutCompleted(state);
final int firstVisibleItemPosition = findFirstVisibleItemPosition();
final int lastVisibleItemPosition = findLastVisibleItemPosition();
int itemsShown = lastVisibleItemPosition - firstVisibleItemPosition + 1;
//if all items are shown, hide the fast-scroller
fastScroller.setVisibility(adapter.getItemCount() > itemsShown ? View.VISIBLE : View.GONE);
}
});
How to know when data is loaded inside custom RecyclerView?
RecyclerView is already calling registerAdapterDataObserver
and it's unregister counterpart on the adapter the supplied adapter.
This observer have several callbacks to let the Recycler know when data is added, removed or changed. You should take advantage of those.
Also, the moment the adapter changes the data, it's not the same moment the views are created/layout on the screen. You might have to call getViewTreeObserver().addOnGlobalLayoutListener
to receive a callback when the children have been layout on the screen.
Hope it helps, happy coding.
Related Topics
Overlay Images Onto Camera Preview Surfaceview
Jar Mismatch! Fix Your Dependencies
Android - Share on Facebook, Twitter, Mail, Ecc
Fragments Destroyed/Recreated with Jetpack's Android Navigation Components
Android Split Action Bar with Action Items on the Top and Bottom
How to Implement the Material-Design Elevation for Pre-Lollipop
How to Add (Vertical) Divider to a Horizontal Linearlayout
Android: How to Detect When a Scroll Has Ended
Android View Not Attached to Window Manager
Android: How to Check If a Particular Accessibilityservice Is Enabled
Android: Collapsingtoolbarlayout and Swiperefreshlayout Get Stuck
Android L's Ripple Effect - Touch Feedback for Buttons - Using Xml
Camera Preview Image Data Processing with Android L and Camera2 API
How to Call a Function After Delay in Kotlin
Android: Can Height of Slidingdrawer Be Set with Wrap_Content
Android - How to Get Install Referrer Programmatically
Android:Select Image from Gallery Then Crop That and Show in an Imageview