RecyclerView+gllide loads a lot of large pictures, fast sliding carton solution

Idea: do not load when sliding, stop sliding and then start loading pictures

insert image description here

This is the working mechanism of RecycleView. We can see that BindViewHolder will be executed when it is not displayed. This is where we call Gilde to load the picture. If the picture is also loaded during the sliding process, it will undoubtedly consume performance. So what should we do? Well, in fact, you only need to rewrite the onScrollStateChanged method of RecycleView to agree whether Gilde can load pictures.

Let's talk about several states of onScrollStateChanged first.

SCROLL_STATE_IDLE 屏幕停止滚动

SCROLL_STATE_DRAGGING 屏幕滚动且用户使用的触碰或手指还在屏幕上

SCROLL_STATE_SETTLING 由于用户的操作,屏幕产生惯性滑动

And Gilde also provides us with two methods

resumeRequests() 开始加载图片

pauseRequests() 停止加载图片

Custom RecyclerView


public class AutoLoadRecyclerView extends RecyclerView {
    
    
    public AutoLoadRecyclerView(Context context) {
    
    
        this(context, null);
    }

    public AutoLoadRecyclerView(Context context, AttributeSet attrs) {
    
    
        this(context, attrs, 0);
    }

    public AutoLoadRecyclerView(Context context, AttributeSet attrs, int defStyle) {
    
    
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
    
    
        addOnScrollListener(new ImageAutoLoadScrollListener());
    }

    //监听滚动来对图片加载进行判断处理
    public class ImageAutoLoadScrollListener extends OnScrollListener {
    
    
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    
    
            super.onScrolled(recyclerView, dx, dy);
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    
    
            super.onScrollStateChanged(recyclerView, newState);
            switch (newState) {
    
    
                case SCROLL_STATE_IDLE: // The RecyclerView is not currently scrolling.
                    //当屏幕停止滚动,加载图片
                    try {
    
    
                        if (getContext() != null) Glide.with(getContext()).resumeRequests();
                    } catch (Exception e) {
    
    
                        e.printStackTrace();
                    }
                    break;
                case SCROLL_STATE_DRAGGING: // The RecyclerView is currently being dragged by outside input such as user touch input.
                    //当屏幕滚动且用户使用的触碰或手指还在屏幕上,停止加载图片
                    try {
    
    
                        if (getContext() != null) Glide.with(getContext()).pauseRequests();
                    } catch (Exception e) {
    
    
                        e.printStackTrace();
                    }
                    break;
                case SCROLL_STATE_SETTLING: // The RecyclerView is currently animating to a final position while not under outside control.
                    //由于用户的操作,屏幕产生惯性滑动,停止加载图片
                    try {
    
    
                        if (getContext() != null) Glide.with(getContext()).pauseRequests();
                    } catch (Exception e) {
    
    
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }
}

Reprint: https://juejin.cn/post/6935743990285205511

Guess you like

Origin blog.csdn.net/gqg_guan/article/details/132109340