Android scrolling Recyclerview when loading images with smoothness optimization

Source: Internet
Author: User

implementation: Use the onscrollstatechanged callback to detect the scrolling state, and set a status value similar to isscrolling inside the recyclerviewadapter to control the loading of the network picture.
Here is a code example:

// Add the following code to the Baseadapter  Public Abstract class Baserecyclerviewadapter<t> extends recyclerview.adapter<recyclerview.viewholder> {    ...     protected false ;    ...      Public void setscrolling (Boolean scrolling) {        = scrolling;    }}

Specific sub-adapter implementations:

//control in Onbind in sub-adapter Public classSubrecyclerviewadapter extends Baserecyclerviewadapter<dataguardranking>{... @Override Public voidOnbindviewholder (Recyclerview.viewholder Viewholder,intposition) {Recyclerviewholder Holder=(Recyclerviewholder) Viewholder; ...if(! Textutils.isempty (Data.getavatarurl ()) &&!isscrolling) {            //Here you can load the library with glide and other network pictures .}Else{Holder.avatarImg.setImageResource (account for bitmap local resources);    } super.onbindviewholder (holder, position); }}

To listen at the UI layer:

//external to Recyclerview Setup monitoringMrecyclerview.addonscrolllistener (NewRecyclerview.onscrolllistener () {@Override Public voidOnscrollstatechanged (Recyclerview Recyclerview,intnewstate) {        //View Source There are three states of State: Scroll_state_idle (stationary), scroll_state_dragging (Ascent), scroll_state_settling (drop)        if(newstate = = Scroll_state_idle) {//Load picture resources when scrolling at rest, greatly improving smoothnessMrecyclerviewadapter.setscrolling (false); Mrecyclerviewadapter.notifydatasetchanged (); //Onbindviewholder responds to the call after notify is called}Elsemrecyclerviewadapter.setscrolling (true);    Super.onscrollstatechanged (Recyclerview, newstate); }}); Mrecyclerview.setadapter (Mrecyclerviewadapter) ;

Android scrolling Recyclerview when loading images with smoothness optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.