Android Pulltorefresh automatic pull-down load refresh implementation

Source: Internet
Author: User

Yesterday, the tester made a bug, that is, as long as the back and forth click the same tab button, the load of data will be repeated, check the code for a long time did not find any problems, and then interrupted the point of debugging, Pulltorefreshlistview.setrefreshing () found to be executed; Self-righteous implementation of the refresh operation (in fact, the refresh operation, but the pull is refreshed, the dropdown is also refreshed, we do when the pull-up refresh to load more), in fact, the control is not able to determine whether the current execution of the operation is a drop-down refresh or pull-up load more, This need to customize a Boolean variable Isautorefresh, through it to identify the action is not a pull-up, the following gives the implementation of the source code:

public class Mytopicfragment extends Fragment implements Mytopicobserver,onitemclicklistener, onrefreshlistener< Listview>,onlastitemvisiblelistener {private int mtype = 0;private Pulltorefreshlistview pulltorefreshlistview; Private ListView actuallistview;private mytopiclistadapter mytopiclistadapter;private Context mcontext;private mytopiclogic logic;private int currentpagecount = 0; The record is currently the first page of the private int totalpagecount; Records the total number of pages The private Boolean isrefreshaction;//flag is the drop-down refresh private mytopicentity mmytopicentity;private LinearLayout promptview;//the prompt to display without any topic private linearlayout mloadinghint = null;private LinearLayout mafterlodinghint = null;  Private ImageView mhintimage = null;private TextView mhinttxt = null;private Boolean isautorefresh = false; @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {Bundle args = This.getarguments (); mtype = Args.getint ("type"); Mcontext = This.getactivity (); logic = new Mytopiclogic (); Logic.addobServer (this); View Rootview = inflater.inflate (R.layout.fragment_pulltorefresh_listview, container, false); initialize (Rootview); return Rootview;} private void Initialize (View rootview) {Pulltorefreshlistview = (Pulltorefreshlistview) Rootview.findviewbyid ( R.id.fragment_pulltorefresh_listview); mytopiclistadapter = new Mytopiclistadapter (); Actuallistview = Pulltorefreshlistview.getrefreshableview (); Registerforcontextmenu (Actuallistview); ActualListView.setAdapter ( Mytopiclistadapter);p Ulltorefreshlistview.setmode (Mode.both);p Ulltorefreshlistview.getloadinglayoutproxy (False , true). Setpulllabel (GetString (R.string.poll_refresh_down_loadmore)); Pulltorefreshlistview.getloadinglayoutproxy (False, True). Setrefreshinglabel (GetString (r.string.poll_refresh_ down_refreshing));p Ulltorefreshlistview.getloadinglayoutproxy (False, True). Setreleaselabel (GetString ( r.string.poll_refresh_down_release));p Ulltorefreshlistview.getloadinglayoutproxy (False, True). Setlastupdatedlabel ("");p Ulltorefreshlistview.setonrefrEshlistener (This);p Ulltorefreshlistview.setonlastitemvisiblelistener (this); Pulltorefreshlistview.setonitemclicklistener (this); mmytopicentity = HealthApplication.mAPPCache.getMyTopic (mtype ); if (mmytopicentity! = null && mmytopicentity.getdata () = null) {Mytopiclistadapter.refresh ( Mmytopicentity.getdata (). GetData ()); currentpagecount = 1;} Promptview = (linearlayout) layoutinflater.from (Mcontext). Inflate (r.layout.net_request_prompt, NULL); Pulltorefreshlistview.setemptyview (Promptview); if (mtype = = 1) {//default shows all my posts mhandler.sendemptymessagedelayed (1, //logic.getmytopic (1, Mtype, "Time");} /* * else if (mtype = = 2) {Logic.getmytopic (ten, 1, Mtype, "Time");} else * if (mtype = = 3) {Logic.getmytopic (ten, 1, Mtype, "Time"),} *///mtype = 1;//set to 1, as long as it is entered I post, has not clicked my reply and my collection, the refresh operation will execute my Posted topic}handler Mhandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {super.handlemessage (msg); Switch (msg.what) {case 1://pulltorefreshlistview.setcurrentmoderefreash (); isautorefresh = true;pulltorefreshlistview.setrefreshing (); break;default:break;}}; @Overridepublic void OnPause () {logic.removeobserver (this); Super.onpause ();} @Overridepublic void Onresume () {if (Logic.getobservers (). Size () = = 0) {logic.addobserver (this);} Super.onresume ();} @Overridepublic void Onitemclick (adapterview<?> listView, View itemview,int position, long Itemviewid) { Mytopicdetail item = (mytopicdetail) mytopiclistadapter.getitem (position-1); Intent Intent = new Intent (Mcontext, Topicd Etailactivity.class); Intent.putextra ("TopicID", Item.getid ()); Startactivityforresult (Intent, 18);} @Overridepublic void Onlastitemvisible () {if (pulltorefreshlistview.getmode () = = Mode.pull_from_start) {// Toast.maketext (Mcontext, "No More Topics", Toast.length_short). Show ();}} @Overridepublic void Onrefresh (pulltorefreshbase<listview> refreshview) {if (Refreshview.isheadershown () | | | Isautorefresh) {Isautorefresh = False;isrefreshaction = True;logic.getmytopic (ten, 1, Mtype, "Time");} else { Isrefreshaction = False;logic.getmytopic (Ten, Currentpagecount + 1, mtype, "Time");}} public void Refresh (int type) {isrefreshaction = True;mtype = type;mhandler.sendemptymessagedelayed (1, 500);// Logic.getmytopic (Ten, 1, Mtype, "Time");} Private class Mytopiclistadapter extends Baseadapter {private arraylist<mytopicdetail> Mlist;public Mytopiclistadapter () {mlist = new arraylist<mytopicdetail> ();} public void Refresh (arraylist<mytopicdetail> ArrayList) {mlist.clear (); mlist = Arraylist;notifydatasetchanged ( );} public void Add (arraylist<mytopicdetail> ArrayList) {mlist.addall (ArrayList); notifydatasetchanged ();} public void Clear () {mlist.clear (); notifydatasetchanged ();} @Overridepublic int GetCount () {return mlist.size ();} @Overridepublic Object getItem (int position) {return mlist.get (position);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder Viewholder = null;if (convertvi EW = = NULL) {Layoutinflater Inflater = Layoutinflater.from (mcontext); Convertview = Inflater.inflate (R.layout.mytopic_list_item , null); Viewholder = new Viewholder (); viewholder.topicname = (TextView) Convertview.findviewbyid (r.id.tv_topic_name); Viewholder.topiccontent = (TextView) Convertview.findviewbyid (r.id.tv_topic_content); viewholder.coloumnname = ( TextView) Convertview.findviewbyid (r.id.column); viewholder.topictime = (TextView) Convertview.findviewbyid ( R.id.sendtime); viewholder.topicgoodcnt = (TextView) Convertview.findviewbyid (R.id.tv_good); viewholder.topicreplycnt = (TextView) Convertview.findviewbyid (R.id.tv_replay); Convertview.settag (ViewHolder);} else {Viewholder = (Viewholder) Convertview.gettag ();} Mytopicdetail item = (mytopicdetail) getItem (position); if (Item.gettitle (). Length () >) {String str = item.gettitle ( ). substring (0, +); ViewHolder.topicName.setText (str + "..."); else {ViewHolder.topicName.setText (Item.gettitle ());} if (item.getcontent (). Length () >) {String temp =(Item.getcontent (). substring (0, ()); ViewHolder.topicContent.setText (temp + "..."); else {ViewHolder.topicContent.setText (item.getcontent ());} if (Item.getsectname (). Length () > 8) {String temp = (item.getsectname (). substring (0, 6)); ViewHolder.coloumnName.setText (temp + "..."); else {ViewHolder.coloumnName.setText (Item.getsectname ());} ViewHolder.topicTime.setText (Item.getcreate_time ()); ViewHolder.topicGoodCnt.setText (item.getgoodcnt () + ""); ViewHolder.topicReplyCnt.setText (item.getdiscusscnt () + ""); return Convertview;} Private class Viewholder {public TextView topicname;//topic name Public TextView topiccontent;//topic content public TextView Coloumnnam e;//column name Public TextView topictime;//topic time public TextView topicgoodcnt;//likes number public TextView topicreplycnt;//number of replies}}@ overridepublic void ongetmytopicsuccess (mytopicentity result) {pulltorefreshlistview.onrefreshcomplete (); Result.getdata () = = null) {Showpromptview (False, GetString (R.string.topic_nodata), 1); Mytopiclistadapter.clear (); PulltorefrEshlistview.setmode (mode.both); currentpagecount = 0; HealthApplication.mAPPCache.setMyTopic (Mtype, ""); Showpromptview (False, Getresources (). getString (R.string.temp_ No_data), 1);} Totalpagecount = Result.getdata (). Getpagecount (); if (isrefreshaction) {Mytopiclistadapter.refresh (Result.getData () . GetData ()); currentpagecount = 1; String label = Dateutils.formatdatetime (Mcontext,system.currenttimemillis (), dateutils.format_show_time| dateutils.format_show_date| Dateutils.format_abbrev_all);p Ulltorefreshlistview.getloadinglayoutproxy (True, false). Setlastupdatedlabel (Label if (1 = = Totalpagecount) {pulltorefreshlistview.setmode (mode.pull_from_start);} else { Pulltorefreshlistview.setmode (Mode.both);}} else {Mytopiclistadapter.add (Result.getdata (). GetData ()); Currentpagecount++;if (Currentpagecount >= Totalpagecount) {Pulltorefreshlistview.setmode (Mode.pull_from_start);}}} @Overridepublic void ongetmytopicfailed (int errorCode, String msg) {pulltorefreshlistview.onrefreshcomplete (); ErrorCode = = () {Showpromptview (False, GetString (R.string.topic_nodata), 1); Mytopiclistadapter.clear (); Pulltorefreshlistview.setmode (mode.both); currentpagecount = 0; HealthApplication.mAPPCache.setMyTopic (Mtype, ""); Showpromptview (False, Getresources (). getString (R.string.temp_ No_data), 1);} else {halotoast.show (mcontext, msg); Showpromptview (False, Getresources (). getString (R.string.req_fail_click_ Refresh), 2);}}            /** * Prompt after loading and loading * * @param isprogressshow * Whether to show progress box * @param text * display of words * @param type * Control which layout is displayed: 0 identity display loading, 1 identity request succeeded, but no data, 2 identity request failed */private void Showpromptview (Boolean isprogressshow, String text, int type {mafterlodinghint = (linearlayout) Promptview.findviewbyid (r.id.after_loding_hint_layout); mLoadingHint = ( LinearLayout) Promptview.findviewbyid (r.id.loding_hint); mhintimage = (ImageView) Promptview.findviewbyid (R.id.hint _image); mhinttxt = (TextView) Promptview.findviewbyid (r.id.hint_txt); View ProgressBar = Promptview.findviewbyid (r.id.net_request_prompT_progressbar); TextView Prompttextview = (TextView) Promptview.findviewbyid (R.id.net_request_prompt_textview); Promptview.setclickable (false); if (type = = 0) {mloadinghint.setvisibility (view.visible); Mafterlodinghint.setvisibility (View.gone); if (isprogressshow) {progressbar.setvisibility (View.VISIBLE);} else { Progressbar.setvisibility (View.gone);} Prompttextview.settext (text);} else if (type = = 1) {mloadinghint.setvisibility (view.gone); mhintimage.setclickable (false); Mhintimage.setimageresource (R.drawable.no_data); Mhinttxt.settext (text); Mafterlodinghint.setvisibility ( view.visible);} else if (type = = 2) {promptview.setclickable (true); mloadinghint.setvisibility (View.gone); Mhintimage.setimageresource (R.drawable.click_refresh_selector); Mhinttxt.settext (text); MHintImage.setClickable ( true); Mhintimage.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {logic.getmytopic (10 , 1, Mtype, "Time");}); Mafterlodinghint.setvisibility (view.visible);}} @Overridepublic void OnactivItyresult (int requestcode, int resultcode, Intent data) {if (Requestcode = = && ResultCode = = Getactivity (). RESULT_OK) {isrefreshaction = True;logic.getmytopic (ten, 1, Mtype, "Time");} Super.onactivityresult (Requestcode, ResultCode, data);}}


Android Pulltorefresh automatic pull-down load refresh implementation

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.