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

That’s how RecycleView works, and you can see that when you’re not displaying it, you’re executing a BindViewHolder and that’s where we call Gilde to load an image so if you load an image in the process of sliding it’s going to be very performance efficient, so what do we do, Just rewrite the RecycleView onScrollStateChanged method to specify whether Gilde can load images.

Let’s start with several states for onScrollStateChanged.

SCROLL_STATE_IDLE Screen Stop scrolling SCROLL_STATE_DRAGGING the screen scrolling and the user's touch or finger is still on the screen SCROLL_STATE_SETTLING the screen slides inertia due to user actionsCopy the code

Gilde also offers two approaches

ResumeRequests () Starts loading pictures. PauseRequests () stops loading picturesCopy the code

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()); } / / listen to scroll to judge the image compression processing 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. // When The screen stops scrolling, load The image try {if (getContext()! = null) Glide.with(getContext()).resumeRequests(); } catch (Exception e) { e.printStackTrace(); } break; case SCROLL_STATE_DRAGGING: // The RecyclerView is being dragged by outside input such as user touch input. // 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. // 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; }}}}Copy the code