Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can RecyclerView preload the first n item on the list?

I have a listing of some item and I am using Glide to load an image onto an ImageView. The problem is, if a particular item, scroll-enter the screen, that is the only time I saw image is being drawn using Glide. This is noticeable when I put crossfade when the image has finished loading.

I know from the start the number of items needed and couple of other items like header and footer. I am thinking of just making a very long view to handle everything and dynamically add those known items at onCreate and just use ScrollView which I have done. I currently implemented a simpler way using a RecyclerView but I having trouble regarding the timing of onBindView call.

What I want is to load first 5 items before they become visible into the screen. This I hope will trigger the method where I bind the image onto the ImageView with Glide before it is fully visible into the screen.

I followed this tutorial and its not working for me. Here is my current setup:

mComicListingAdapter = new ComicListingsAdapter(getContext(), Constants.CHAPTERS, (ComicListingsAdapter.Callback) getActivity());
mRecycleViewHomeScreen.setAdapter(mComicListingAdapter);
mRecycleViewHomeScreen.setHasFixedSize(true);
mRecycleViewHomeScreen.setItemViewCacheSize(42);
mRecycleViewHomeScreen.setLayoutManager(pcll);

Is this possible in RecyclerView? Or maybe I should try dynamically creating ImageView and appending them to a parent View as I have done so. It looks great but there is a lot of things to do in my case using this method. I am just curious if I can direct RecyclerView to load n-amount of items in advance.

Thanks!

like image 474
Neon Warge Avatar asked Oct 16 '25 16:10

Neon Warge


1 Answers

I've found a blog post where author suggest to extend LinearLayoutManager and override getExtraLayoutSpace().

And it worked. More items were preloaded depending on the number of pixels returned in getExtraLayoutSpace().

P.S. I've not checked yet how that affects performance, especially on pre-lollipop devices.

Sample:

val customLayoutManager = CustomLayoutManager(this)
recyclerView = findViewById<RecyclerView>(R.id.recyclerView).apply {
    layoutManager = customLayoutManager
    ... // 
}

class CustomLayoutManager(context: Context?) : LinearLayoutManager(context) {
    override fun getExtraLayoutSpace(state: RecyclerView.State?) = 2000
} 
like image 192
Alexey Avatar answered Oct 18 '25 06:10

Alexey