Custom Load More Recycleview

Source: Internet
Author: User

Custom Load More Recycleview

<span style= "FONT-SIZE:18PX;"    >public class Loadingrecyclerview extends Recyclerview {static final String TAG = "Loadingrecyclerview";    Private Loadingmorelistener Mmorelistener; private static final int NONE = 0;//idle state private static final int LOADING = 1;//load state private static final int Loadin    G_error = 2;//Load state private static final int no_more_data = 3;//No more data private static final Boolean DEBUG = false;        private int mstate = 0;//load state public Loadingrecyclerview (context context) {super (context);    Init ();        } public Loadingrecyclerview (context context, AttributeSet Attrs) {Super (context, attrs);    Init (); } public Loadingrecyclerview (context context, AttributeSet attrs, int defstyle) {Super (context, Attrs, Defstyle        );    Init ();  } private void Init () {Addonscrolllistener (new Onscrolllistener () {@Override public void Onscrollstatechanged (Recyclerview recyclerview, int NewstATE) {super.onscrollstatechanged (Recyclerview, newstate); if (newstate = = scroll_state_dragging) {if (!isloading () && canscroolloadingmore () && m                    state = = Loading_error) {reloading (); }}} @Override public void onscrolled (recyclerview recyclerview, int dx,                int dy) {super.onscrolled (recyclerview, DX, dy); if (islastpositionvisible () &&!isloading () && Canscroolloadingmore ()) {//turns to last, and the current status is not                    For loading, load if (DEBUG) {LOG.E (TAG, "turned to last");                } reloading ();    }            }        }); }/** * Can load more * * @return */Private Boolean canscroolloadingmore () {if (mstate = = Loadin G_error | |        Mstate = = No_more_data) {return false; } return true;    }/** * Load complete */public void Onloadingcomplete () {mstate = NONE;        /** * Load Failed * * @return */public void Onloadingerror () {mstate = Loading_error;    Getloadingmoreadapter (). Loadingerror ();                 }/** * Reload * * public void reloading () {if (!isloading ()) {if (Mmorelistener! = null) {                Mstate = LOADING;                Getloadingmoreadapter (). Resetloadingmore ();            if (Mmorelistener! = null) Mmorelistener.onloadingmore (); }}}/** * Reset load in Layout */public void Resetloadingview () {getloadingmoreadapter (). Resetloadin    Gmore (); }/** * No more data * * @return */public void Nomoredata () {getloadingmoreadapter (). Nomoredata (    );    } public Loadingmoreadapter Getloadingmoreadapter () {return (Loadingmoreadapter) getadapter (); } public Loadingmoreadapter.loadingmoreholder GetlOadingmoreholder () {//Get the item to be updated with view view view = Getchildat (Getchildcount ()-1);         if (null! = Getchildviewholder (view)) {return (Loadingmoreadapter.loadingmoreholder) getchildviewholder (view);    } return null;        } public boolean islastpositionvisible () {int itemCount = Getadapter (). GetItemCount ();    return ItemCount = = Getlastvisibleposition () + 1; }/** * Sets if there are more data markers * * @param isnomore */public void Setnomoredata (Boolean isnomore) {if (Isnomore)            {mstate = No_more_data;        Nomoredata ();        } else {mstate = NONE;     }}/** * is loaded in:    * * @return */public boolean isloading () {return mstate = = LOADING;        }/** * Gets the position of the last display * * @return */private int getlastvisibleposition () {int position; if (Getlayoutmanager () instanceof Linearlayoutmanager) {position = ((linearlayouTmanager) Getlayoutmanager ()). Findlastvisibleitemposition (); } else if (Getlayoutmanager () instanceof Gridlayoutmanager) {position = ((Gridlayoutmanager) Getlayoutmanager (        ). Findlastvisibleitemposition ();  } else if (Getlayoutmanager () instanceof Staggeredgridlayoutmanager) {Staggeredgridlayoutmanager LayoutManager            = (Staggeredgridlayoutmanager) Getlayoutmanager ();            int[] lastpositions = layoutmanager.findlastvisibleitempositions (New Int[layoutmanager.getspancount ());        Position = Getmaxposition (lastpositions);        } else {position = Getlayoutmanager (). GetItemCount ()-1;        } if (DEBUG) {log.e (TAG, Position + "");    } return position;        }/** * Get the maximum position * * @param positions * @return */private int getmaxposition (int[] positions) {        int size = Positions.length;        int maxposition = Integer.min_value;  for (int i = 0; i < size; i++) {          Maxposition = Math.max (maxposition, positions[i]);    } return maxposition;    } public void Setloadingmorelistener (Loadingmorelistener listener) {Mmorelistener = listener;    } public interface Loadingmorelistener {public void Onloadingmore (); }}</span>

load More Recycleviewadapter

<span style= "FONT-SIZE:24PX;"    >public abstract class Loadingmoreadapter<t> extends Recyclerview.adapter {private list<t> List;    public static final int ITEM = 0, Loading_more = 1, No_more_data = 2, loading_error = 3;    public int mstate = Loading_more;    protected context context;        Public Loadingmoreadapter (context context, list<t> List) {this.context = context;    This.list = list; } @Override public int getitemviewtype (int position) {if (position = = List.size ()) {return Mstat        E    } return ITEM; } @Override Public Recyclerview.viewholder oncreateviewholder (viewgroup parent, int viewtype) {switch (VIEWT ype) {case Loading_more://Load more return new Loadingmoreholder (Layoutinflater.from            (context). Inflate (R.layout.layout_loading_more, parent, false)); Case No_more_data://No more data Loadingmoreholder NomoredataholDer = new Loadingmoreholder (Layoutinflater.from (context). Inflate (R.layout.layout_loading_more, parent, false));                Nomoredataholder.settext ("No more Data");                Nomoredataholder.visibleprogressbar (FALSE);            return nomoredataholder; Case LOADING_ERROR://Load Error Loadingmoreholder Errorholder = new Loadingmoreholder (layoutinfla                Ter.from (context). Inflate (R.layout.layout_loading_more, parent, false));                Errorholder.settext ("Load failed, click Reload");                Errorholder.visibleprogressbar (FALSE);            return errorholder;        Case Item:return Getitemholder ();    } return null; } @Override public void Onbindviewholder (final recyclerview.viewholder holder, int position) {if (position =                = List.size ()) {//Load more Holder.itemView.setOnClickListener (new View.onclicklistener () { @Override public void OnClick (View V{if (holder.itemView.getParent () instanceof Loadingrecyclerview) {if (mstate = = Loading_error) {Loadingrecyclerview Recyclerview = (loadingrecyclerview) holder.itemview.g                            Etparent ();                        Recyclerview.reloading ();        }                    }                }            });        } else {Setitemholder (holder, position);    }} @Override public int getitemcount () {//+1 = load more layouts return list = = null? 0:list.size () + 1;        }/** * No more data */public void Nomoredata () {mstate = No_more_data;    Notifydatasetchanged ();        /** * Load Data * @return */public void Resetloadingmore () {mstate = Loading_more;    Notifydatasetchanged ();        /** * Load Data * @return */public void Loadingerror () {mstate = Loading_error;    Notifydatasetchanged (); } public Abstract Recyclerview.viewholder Getitemholder ();    public abstract void Setitemholder (recyclerview.viewholder holder, int position);        /** * Loading in Layout */class Loadingmoreholder extends Recyclerview.viewholder {TextView tv_text;        ProgressBar Progress_bar;            Public Loadingmoreholder (View Itemview) {super (Itemview);            Tv_text = (TextView) Itemview.findviewbyid (R.id.tv_text);        Progress_bar = (ProgressBar) Itemview.findviewbyid (R.id.progress_bar);        } public void SetText (String msg) {tv_text.settext (msg); } public void Visibleprogressbar (Boolean isVisible) {if (isVisible) {PROGRESS_BAR.SETVI            Sibility (view.visible);            } else {progress_bar.setvisibility (view.gone); }}}}</span>



Custom Load More Recycleview

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.