ListView implementation Drop-down Refresh load More instance code (directly to use) _android

Source: Internet
Author: User
Tags object object static class

ListView Api Bixu take a good look.

Mnewsadapter.notifydatasetchanged ()//Refresh ListView

Custom Refreashlistview

Package Com.itguang.dell_pc.myapplication.view;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;
Import android.view.animation.Animation;
Import android.view.animation.RotateAnimation;
Import Android.widget.AbsListView;
Import Android.widget.ImageView;
Import Android.widget.ListView;
Import Android.widget.ProgressBar;
Import Android.widget.TextView;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
/** * Author: small brother on 2016/7/7.        * e-mail: * Revision History: * Description: *┏┓┏┓*┏┛┻━━━┛┻┓━━━━┻┓*┃┃*┃━┃* ┃> <┃*┃┃*┃ ... ⌒ ...  ┃*┃┃*┗━┓┏━┛*┃┃code is far away from bug with the animal protecting *┃ History ┃ God beast Bless, code no bug *┃ ┃*┃ ┃*┃ pet ┃*┃┃*┃┗━━━┓*┃bug days     Enemy ┣┓┣┓┣┓┣┓┣┓*     ┃┏┛*┗┓┓┏━┳┓┏┛*┃┫┫┃┫┫*┗┻┛┗┻┛*/public class Refreshlistview extends Listv  Iew implements Abslistview.onscrolllistener {private static final int state_pull_refresh = 0;//dropdown refresh private static final int state_release_refresh = 1;//release refresh private static final int state_refreshing = 2;//refreshing private int mcurrrentstate =
state_pull_refresh;//Current state private View Mheaderview;
Private TextView Tvtitle;
Private TextView Tvtime;
Private ProgressBar pbprogress;
Private ImageView Ivarrow;
Private Rotateanimation Animup;
Private Rotateanimation Animdown;
private int starty = -1;//sliding start of the y-coordinate private int endY;
private int measuredheight;
private int mfooterviewheight;
Private View Footerview; Public Refreshlistview {Super (context); Initheaderview (), Initfooterview ();} Public Refreshlistview ( Context context, AttributeSet Attrs) {Super (context, attrs); Initheaderview (); Initfooterview ();} public Refreshlistvie W (Context, attributESet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); Initheaderview (); Initfooterview ();}/** * Initialization header Layout * *
private void Initheaderview () {Mheaderview = View.inflate (GetContext (), r.layout.refeeash_header, NULL);
This.addheaderview (Mheaderview);
Tvtitle = (TextView) Mheaderview.findviewbyid (;
Tvtime = (TextView) Mheaderview.findviewbyid (;
Pbprogress = (ProgressBar) Mheaderview.findviewbyid (;
Ivarrow = (ImageView) Mheaderview.findviewbyid (;
Mheaderview.measure (0, 0);//first measure and then get its height measuredheight = mheaderview.getmeasuredheight ();
Mheaderview.setpadding (0,-measuredheight, 0, 0);
Initarrowanim ();
Tvtime.settext ("Last Refresh Time:" + getcurrenttime ());}  /** * Initialize the foot layout */public void Initfooterview () {Footerview = View.inflate (GetContext (), R.layout.refreash_listview_footer,
This.addfooterview (Footerview);
Footerview.measure (0, 0);
Mfooterviewheight = Footerview.getmeasuredheight (); Footerview.setpadding (0,-mfooterviewheigHT, 0, 0);//default hidden foot layout This.setonscrolllistener (this);
Private Boolean Isloadingmore; @Override public void onscrollstatechanged (Abslistview view, int scrollstate) {if (scrollstate = = Scroll_state_fling | | s Crollstate = = Scroll_state_idle) {if (getlastvisibleposition () = = GetCount ()-1 &&!isloadingmore) {//Slip last Syste
M.out.println ("In the End ...");
Footerview.setpadding (0, 0, 0, 0);
SetSelection (GetCount ()-1);//change the display position of ListView Isloadingmore = true;
if (Mlistener!= null) {Mlistener.onloadmore ();}} @Override public void Onscroll (Abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {} Override public boolean ontouchevent (motionevent ev) {switch (ev.getaction ()) {Case MotionEvent.ACTION_DOWN:startY = (i
NT) Ev.getrawy ();
Break Case MotionEvent.ACTION_MOVE:if (Starty = = 1) {//sometimes does not respond to Motionevent.action_down events, this is to retrieve starty coordinates starty = (int)
Trawy (); ///When refreshing, jump out of the loop and no longer execute the following logical if (mcurrrentstate = = State_release_refresh) {break;}Y = (int) Ev.getrawy (); int dy = endy-starty;//calculates the finger slip distance if (dy > 0 && getfirstvisibleposition () = = 0) {//Only dropdown and is currently the first item to allow dropdown int padding = dy-measuredheight;//Compute padding mheaderview.setpadding (0, padding, 0, 0);/set current padding if (padding > 0 && Amp  Mcurrrentstate!= state_release_refresh) {mcurrrentstate = State_release_refresh; Refreshstate ();} else if (padding < 0 && mcurrrentstate!= state_pull_refresh) {///dropdown refresh Status mcurrrentstate = State_pull_refresh; Refreshstate ();} RE
Turn true;
} break; Case MotionEvent.ACTION_UP:startY = -1;//finger lift//When the state is released when the refresh is lifted the finger is being refreshed if (mcurrrentstate = = State_release_refresh) {MCu
Rrrentstate = state_refreshing;//is refreshing mheaderview.setpadding (0, 0, 0, 0);//display refreshstate ();
else if (mcurrrentstate = = State_pull_refresh) {mheaderview.setpadding (0,-measuredheight, 0, 0);//Hide} break;
return super.ontouchevent (EV); Private String GetCurrentTime () {SimpleDateFormat format = new SimpleDateFormat("Yyyy-mm-dd HH:mm:ss");
String currenttime = Format.format (New Date ());
return currenttime; } private void Initarrowanim () {//init arrow Animation animup = new Rotateanimation (0, -180, animation.relative_to_self, 0.5f, Animati On.
Relative_to_self, 0.5f);
Animup.setduration (500); Animup.setfillafter (TRUE);//Hold status//arrow down animation Animdown = new Rotateanimation ( -180, 0, animation.relative_to_self, 0.5f,
Animation.relative_to_self, 0.5f);
Animdown.setduration (500);
Animdown.setfillafter (TRUE); /** * Refresh the layout of the Drop-down control * * private void Refreshstate () {switch (mcurrrentstate) {case STATE_PULL_REFRESH:tvTitle.setText ("dropdown brush
New ");
Ivarrow.setvisibility (view.visible);
Pbprogress.setvisibility (view.invisible);
Ivarrow.startanimation (Animdown);
Case STATE_RELEASE_REFRESH:tvTitle.setText ("loosen refresh");
Ivarrow.setvisibility (view.visible);
Pbprogress.setvisibility (view.invisible);
Ivarrow.startanimation (ANIMUP);
Case STATE_REFRESHING:tvTitle.setText ("refreshing ..."); Ivarrow.clearanimation ();//You must clear the animation before you can hide the Ivarrow.setvisiBility (view.invisible);
Pbprogress.setvisibility (view.visible);
if (Mlistener!= null) {Mlistener.onrefreash ();} break;
}} Onrefreashlistener Mlistener; public void Setonrefreashlistener (Onrefreashlistener listener) {Mlistener = listener;} public Interface Onrefreashliste ner {void Onrefreash (); void Onloadmore ()}/** * Pull Drop-down Refresh control/public void Onrefreashcomplete () {if (Isloadingmore) {f
Ooterview.setpadding (0,-mfooterviewheight, 0, 0);//Hide foot layout Isloadingmore = false;
else {mcurrrentstate = State_pull_refresh; Tvtitle.settext ("Drop-down refresh"); Ivarrow.setvisibility (view.visible);
Pbprogress.setvisibility (view.invisible); Mheaderview.setpadding (0,-measuredheight, 0, 0);//Hidden}} public void Nofooterview () {footerview.setpadding (0, Mfootervi Ewheight, 0, 0);//Hide Foot Layout}}

Reference Refreashlistview

in the


Package com.itguang.dell_pc.myapplication.base;
Import Android.text.TextUtils;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.ImageView;
Import Android.widget.ListView;
Import Android.widget.TextView;
Import Android.widget.Toast;
Import Com.itguang.dell_pc.myapplication.domain.NewsData;
Import Com.itguang.dell_pc.myapplication.domain.TabData;
Import com.itguang.dell_pc.myapplication.globe.GlobeContents;
Import Com.itguang.dell_pc.myapplication.view.RefreshListView;
Import Com.lidroid.xutils.BitmapUtils;
Import Com.lidroid.xutils.HttpUtils;
Import Com.lidroid.xutils.ViewUtils;
Import com.lidroid.xutils.exception.HttpException;
Import Com.lidroid.xutils.http.ResponseInfo;
Import Com.lidroid.xutils.http.callback.RequestCallBack; ImportCom.lidroid.xutils.http.client.HttpRequest;
Import Com.lidroid.xutils.view.annotation.ViewInject;
Import Com.viewpagerindicator.CirclePageIndicator;
Import java.util.ArrayList;
/** * Author: small brother on 2016/4/22.        * e-mail: * Revision History: * Description: *┏┓┏┓*┏┛┻━━━┛┻┓━━━━┻┓*┃┃*┃━┃* ┃> <┃*┃┃*┃ ... ⌒ ...  ┃*┃┃*┗━┓┏━┛*┃┃code is far away from bug with the animal protecting *┃ History ┃ God beast Bless, code no bug *┃ ┃*┃ ┃*┃ pet ┃*┃┃*┃┗━━━┓*┃bug days  Enemy ┣┓┣┓┣┓┣┓┣┓*┃┏┛*┗┓┓┏━┳┓┏┛*┃┫┫┃┫┫*┗┻┛┗┻┛*//** * Also sign details page/public Class Tabdetailpager extends Basemenudetailpager implements Viewpager.onpagechangelistener {/** * @param activity * @par
Am Newstabdata * * Newsdata.newstabdata mtabdata;
Private String Murl;
Private Tabdata Mtabdetaildata; @ViewInject ( PRIvate Viewpager Mviewpager; @ViewInject ( Private Refreshlistview newslistview;//News listing @ViewInject ( TextView tvtitle;//News title @ViewInject ( Private circlepageindicator indicator;//Headline News indicator private ArrayList
<TabData.TopNewsData> mtopnewslist;//News data collection private int[] images;
Private arraylist<tabdata.tabnewsdata> mnewslist;//News data collection private String Mmoreurl;
Private Newsadapter Mnewsadapter;
Public Tabdetailpager (activity activity, Newsdata.newstabdata Newstabdata) {super (activity); mtabdata = Newstabdata;
Murl = Globecontents.server_url + Mtabdata.url;  @Override public View initviews () {images = new int[]{r.drawable.topnews1, R.DRAWABLE.TOPNEWS21, R.DRAWABLE.TOPNEWS31,
View view = View.inflate (mactivity, R.layout.tab_detail_pager, NULL); View Headerview = view.inflate (mactivity, r.layout.list_header_topnews, NULL);//Load header layout//TextView Tvtitle = (TextView) VI
Ew.findviewbyid (; Event Injection VIEWUTILS.INject (this, view);
Viewutils.inject (this, headerview);
Mviewpager.addonpagechangelistener (this);
The fat headline is loaded into the ListView Newslistview.addheaderview (Headerview) in the form of head layout. Set Drop-down Refresh Listener Newslistview.setonrefreashlistener (new Refreshlistview.onrefreashlistener () {@Override public void Onrefreash () {Getdatafromserver ():} @Override public void Onloadmore () {if (Mmoreurl!= null) {Getmoredatafromserver ()
; else {toast.maketext (mactivity, "No more Data", Toast.length_short). Show (); Newslistview.onrefreashcomplete ();//
Newslistview.nofooterview ();
return view;  @Override public void InitData () {getdatafromserver ();//This is an asynchronous,} private void Getdatafromserver () {Httputils utils =
New Httputils (); Utils.send (HttpRequest.HttpMethod.GET, Murl, New requestcallback<string> () {@Override public void onsuccess ( Responseinfo<string> responseinfo) {string result = (string) Responseinfo.result;//System.out.println ("
Page Sign Details page returns results: +++++++++++++++++++ "+ result";
Prasedata (result, false); NewsListview.onrefreashcomplete (); @Override public void OnFailure (HttpException E, String s) {Toast.maketext (mactivity, "Get Tabdetaildata failed!", Toast.leng
Th_short). Show ();
Error.printstacktrace ();
}); /** * Load Next page data */private void Getmoredatafromserver () {httputils utils = new Httputils (); Utils.send (HTTPREQUEST.HTTPME Thod. Get, Mmoreurl, new requestcallback<string> () {@Override public void onsuccess (responseinfo<string> Responseinfo) {String result = (string) Responseinfo.result;//System.out.println ("Page sign Details page returns: +++++++++++++++++++" +
Prasedata (result, true);
Newslistview.onrefreashcomplete (); @Override public void OnFailure (HttpException E, String s) {Toast.maketext (mactivity, "Get Tabdetaildata failed!", Toast.leng
Th_short). Show ();
Error.printstacktrace ();
}); /** * Parses a JSON string into a JSON object * * @param result to parse the JSON string */private void Prasedata (String result, Boolean Isloadmore) {G
Son Gson = new Gson (); Mtabdetaildata = Gson.fromjson (result, Tabdata.class)//Returns the Tabdata object System.out.println ("page sign Details page: +++++++++++++++++++++" + mtabdata);
Process more pages of logic final String more =; if (! Textutils.isempty (more)) {Mmoreurl = Globecontents.server_url + more;} else {mmoreurl = null;} if (!isloadmore) {Mtop
Newslist =;
Mnewslist =;
if (mtopnewslist!= null) {Mviewpager.setadapter (New Topnewsadapter ()); Indicator.setonpagechangelistener (this); Indicator.setviewpager (Mviewpager)//After setting up the adapter, set indicator Indicator.setsnap (TRUE);/set snapshot display indicator.onpageselected (0
//indicator will be smart, leaving will record the current position tvtitle.settext (Mtopnewslist.get (0). title);
} mnewsadapter = new Newsadapter (); Populate the News list if (mnewslist!= null) {newslistview.setadapter (mnewsadapter);}}
else {//If the next page is loaded, you need to append the data to the original set arraylist<tabdata.tabnewsdata> news =;
Mnewslist.addall (news); Mnewsadapter.notifydatasetchanged ()///Refresh ListView}} @Override public void onpagescrolled (int position, float PositionoffSet, int positionoffsetpixels) {} @Override public void onpageselected (int position) {//update headline headlines Tabdata.topnewsdata TOPN
Ewsdata = Mtopnewslist.get (position);
Tvtitle.settext (Topnewsdata.title);  @Override public void onpagescrollstatechanged (int state) {}/** * ListView Adapter/class Newsadapter extends Baseadapter
{private final bitmaputils utils; Newsadapter () {utils = new bitmaputils (mactivity); Utils.configdefaultloadingimage (R.drawable.pic_item_list_default
); @Override public int GetCount () {return mnewslist.size ():} @Override public Object getitem (int position) {return mnew
Slist.get (position); @Override public long getitemid (int position) {return position;} @Override public view getview (int position, View conv Ertview, ViewGroup parent) {Viewholder holder; if (Convertview = null) {Convertview = View.inflate (mactivity, R.layout.
List_news_item, NULL);
Holder = new Viewholder ();
Holder.ivpic = (ImageView) Convertview.findviewbyid (; Holder.tvtitle = (TEXTVIEW) Convertview.findviewbyid (;
Holder.tvdate = (TextView) Convertview.findviewbyid (;
Convertview.settag (holder);
else {holder = (Viewholder) Convertview.gettag ();}
Tabdata.tabnewsdata item = (tabdata.tabnewsdata) getitem (position);
Holder.tvTitle.setText (Item.title);
Holder.tvDate.setText (item.pubdate);
Utils.display (Holder.ivpic, item.listimage);
return convertview; } static class Viewholder {public TextView tvtitle, public TextView tvdate, public imageview ivpic;/** * Viewpager fit
Device/class Topnewsadapter extends Pageradapter {private bitmaputils bitmaputils; Topnewsadapter () {bitmaputils = new bitmaputils (mactivity); Bitmaputils.configdefaultloadingimage ( R.drawable.topnews_item_default/set Default picture} @Override public int getcount () {return (
); @Override public boolean isviewfromobject (view view, Object object) {return view = = object;} @Override public Object I Nstantiateitem (ViewGroup container, int position) {IMageview ImageView = new ImageView (mactivity);
Imageview.setimageresource (Images[position]); Imageview.setscaletype (ImageView.ScaleType.FIT_XY); fills the picture//Tabdata.topnewsdata based on the size of the control Topnewsdata =
Mtopnewslist.get (position);
Bitmaputils.display (Imageview, (position). URL);
Container.addview (ImageView);
return ImageView; @Override public void Destroyitem (ViewGroup container, int position, object) {Container.removeview (View) object
); }

The above is a small set to introduce the ListView implementation Drop-down Refresh load More instance code (directly to use) The full description, hope to be helpful to everyone, if you have any questions welcome to my message, small series will promptly reply to everyone, here also thank you for the cloud Habitat Community website support!

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: 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.