I\'m using the support RecyclerView in my app, and I see the most bizarre thing. It doesn\'t display any items until I touch to scroll. Then, all of a sudden, the RecyclerView p
So at first, just like everybody else I just added:
recyclerView.smoothScrollToPosition(0)
and it worked pretty much good enough, however it wasn't nice and you had to remember to add it every time. and I then I followed @SudoPlz comment and answer on another question and it also worked too, you have to extend RecyclerView and override requestLayout:
private boolean mRequestedLayout = false;
@SuppressLint("WrongCall")
@Override
public void requestLayout() {
super.requestLayout();
// We need to intercept this method because if we don't our children will never update
// Check https://stackoverflow.com/questions/49371866/recyclerview-wont-update-child-until-i-scroll
if (!mRequestedLayout) {
mRequestedLayout = true;
this.post(() -> {
mRequestedLayout = false;
layout(getLeft(), getTop(), getRight(), getBottom());
onLayout(false, getLeft(), getTop(), getRight(), getBottom());
});
}
}
while still, I would have preferred this to fixed after 4, 5 years, however, this was a good workaround, and you won't forget about them in your view.
Since this is still happening, here is an specific fixed instance in case it helps someone:
The fix:
You would have to run the stuff you do in onPostExecute on the UI thread so that the RecyclerView gets redrawn. That's why the smooth scrolling works, because that is running on the UI thread and therefore causing the view to redraw.
It's most likely because you're not calling the correct notification methods of RecyclerView.Adapter. You have a much more granular interface for this than you previously had in ListAdapters. For example, in addAll() you should call notifyItemRangeInserted(oldConversationsSize, conversations.size())
instead of notifyDataSetChanged
If anyone else is having this issue using RxJava and Retrofit, I solved this issue by adding the .observeOn(AndroidSchedulers.mainThread())
operator into my method chain before subscribing. I had read this was taken care of by default, and thus not explicitly necessary, but I guess not. Hope this helps.
Example:
public void loadPhotos() {
mTestPhotoService.mServiceAPI.getPhotos()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(photoList -> mRecyclerActivity.OnPhotosLoaded(photoList));
}
This is a bit late, but in my case I had a recylerview inside a viewpager (TabView) that was inside another viewpager (using BottomNavigationView) and smooth scrolling didn't change anything.
However, I noticed that I had the recylerview set with Visibility GONE when the layout was drawn, so the solution was:
new Handler(Looper.getMainLooper()).postDelayed(() -> {
binding.recyclerView.scrollToPosition(0);
}, getResources().getInteger(android.R.integer.config_shortAnimTime));