Android ListView slides to automatically load data. Modify multiple places, remove redraw listvew, prevent flicker

Source: Internet
Author: User

Thanks to other great gods for source support
I only make changes and comments on this source code.
How to use:
Set Load Paging data listener
Loadinglistview.setonloadmorelistener (New Onloadmorelistener () {
public void Onloadmore () {
if (page<3) {
}else{
Loadinglistview.onloadmorecomplete ();//Remove load more layouts, load complete
Loadinglistview.setisenable (FALSE); Ture: Allow to open down, automatically load data, false not allow slide load data
}
}

});



The whole code is simpler, only one class.

Import Android.content.context;import Android.util.attributeset;import Android.util.log;import Android.view.layoutinflater;import Android.view.view;import Android.widget.abslistview;import Android.widget.abslistview.onscrolllistener;import Android.widget.listadapter;import Android.widget.ListView; Import Android.widget.progressbar;import Android.widget.relativelayout;import Com.ferris.ferrisapp.r;public class Loadmorelistview extends ListView implements Onscrolllistener {private static final String TAG = "Loadmorelistview";/** * Listener that would receive notifications every time the list scrolls. */private onscrolllistener monscrolllistener;private layoutinflater minflater;private Boolean isEnable = true;public Boolean getisenable () {return isenable;} public void setisenable (Boolean isenable) {this.isenable = isenable;} Footer Viewprivate relativelayout mfooterview;//private TextView mlabloadmore;private ProgressBar mprogressbarloadmore;//Listener to process load more items if useR reaches the end of the listprivate Onloadmorelistener monloadmorelistener;//to know if the list was loading more ITEMSPR Ivate Boolean misloadingmore = false;private int Mcurrentscrollstate;public Loadmorelistview (context context) {Super ( context); init (context);} Public Loadmorelistview (context context, AttributeSet Attrs) {Super (context, attrs); init (context);} Public Loadmorelistview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); Init ( context);} private void init (context context) {Minflater = (layoutinflater) context.getsystemservice (context.layout_inflater_ SERVICE); Footermfooterview = (relativelayout) minflater.inflate (R.layout.load_more_footer, this, false);//Load Bottom view/* * Mlabloadmore = (TextView) Mfooterview *. Findviewbyid (R.id.load_more_lab_view); */mprogressbarloadmore = (ProgressBar) Mfooterview.findviewbyid (R.id.load_more_progressbar); Addfooterview ( Mfooterview); Super.setonscrolllistener (this);//Set scrolling listener} @Overridepublic void Setadapter (ListadApter adapter) {Super.setadapter (adapter);} /** * Set The listener that would receive notifications every time the list * scrolls. * * @param l * the scroll listener. */@Overridepublic void Setonscrolllistener (Abslistview.onscrolllistener l) {monscrolllistener = l;} /** * Register A callback to being invoked when this list reaches the end (last * item is visible) * * @param onloadmorelist Ener * The callback to run. */public void Setonloadmorelistener (Onloadmorelistener onloadmorelistener) {Monloadmorelistener = OnLoadMoreListener ;} public void Onscroll (Abslistview view, int firstvisibleitem,int visibleitemcount, int totalitemcount) {if (isenable) {//Yes No turn on sliding if (monscrolllistener! = null) {Monscrolllistener.onscroll (view, Firstvisibleitem,visibleitemcount, Totalitemcount);} if (Monloadmorelistener! = null) {if (VisibleItemCount = = Totalitemcount) {//is just covered with a screen, Mprogressbarloadmore.setvisibility (view.gone); return;} Boolean loadmore = Firstvisibleitem + visibleitemcount >= totalitemcount;//swipe to bottom if (!misloadingmore && loadmore&& mcurrentscrollstate! = scroll_state_idle) {// If you scroll to the bottom, and you have stopped scrolling, we'll show you a load more. Mprogressbarloadmore.setvisibility (view.visible); misloadingmore = true; Onloadmore ();//Callback Listener}}}}/** * Disable swipe, view redraw, otherwise cause blinking */public void onscrollstatechanged (abslistview view, int scrollstate) { Bug Fix:listview was wasn't clickable after scroll//because I didn't need Listvie item to press. So I blocked this out, otherwise it would be possible to load the picture will blink constantly oh. This place should be noticed. if (isenable) {//whether to turn on sliding//if (scrollstate = = Onscrolllistener.scroll_state_idle) {//view.invalidateviews ();/} Mcurrentscrollstate = Scrollstate;if (Monscrolllistener! = null) {monscrolllistener.onscrollstatechanged (view, scrollstate);}}} public void Onloadmore () {LOG.D (TAG, "Onloadmore"), if (monloadmorelistener! = null) {Monloadmorelistener.onloadmore ();}} /** * Notify The loading more operation have finished */public void Onloadmorecomplete () {Misloadingmore = FALSE;MPROGRESSB Arloadmore.setvisibility (View.gone);} /** * Interface DefinitionFor a callback to is invoked when list reaches the last item (the user load more items in the list) */public interface O  Nloadmorelistener {/** * Called when the list reaches the last item (the last item was visible * to the user) */public void Onloadmore ();}}




Android ListView slides to automatically load data. Modify multiple places, remove redraw listvew, prevent flicker

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.