Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Recyclerview preload views

Tags:

Im wondering how I can avoid the little white 'flashing' of the views in a recyclerview when the user scrolls a little faster.
The flashing can be avoided of course by preloading more views outside of the visible screen

I could not find anything yet how this can be done, although this must be a pretty common task??

I tried this code from a blog:

public class PreCachingLayoutManager extends LinearLayoutManager {
    private static final int DEFAULT_EXTRA_LAYOUT_SPACE = 600;
    private int extraLayoutSpace = -1;
    private Context context;

    public PreCachingLayoutManager(Context context) {
        super(context);
        this.context = context;
    }

    public PreCachingLayoutManager(Context context, int extraLayoutSpace) {
        super(context);
        this.context = context;
        this.extraLayoutSpace = extraLayoutSpace;
    }

    public PreCachingLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
        this.context = context;
    }

    public void setExtraLayoutSpace(int extraLayoutSpace) {
        this.extraLayoutSpace = extraLayoutSpace;
    }

    @Override
    protected int getExtraLayoutSpace(RecyclerView.State state) {
        if (extraLayoutSpace > 0) {
            return extraLayoutSpace;
        }
        return DEFAULT_EXTRA_LAYOUT_SPACE;
    }
}

Then I assigned the LayoutManager to my custom Recyclerview in the constructor by using setLayoutManager()
It is "custom", but I only wanted to set the LayoutManager in the costructor, thats why I overwrote the RecyclerView
Unfortunately, this did not have any effect

like image 209
skaldesh Avatar asked Nov 01 '16 10:11

skaldesh


1 Answers

The RecyclerView is a very efficient API already and usually if you have frames dropping, you can optimize your item layout to be lighter and also make sure you free resources when views are recycled. There is really no need to pre-load anything.

Here is a blog post that gives some ideas of how to do those things https://blog.workable.com/recyclerview-achieved-60-fps-workables-android-app-tips/

They don't mention it in this post, but also if your items have images, make sure those images are not too big.

The main point is to make the drawing of your views as fast as possible because everything is constantly redrawn as you scroll.

like image 79
JDenais Avatar answered Sep 22 '22 16:09

JDenais