Android third-party framework Xlistview use

Source: Internet
Author: User
Tags php write

Now the pull-up refresh, drop-down load more is already a voluminous app standard

Recently, there is a related need to learn a bit, or the old saying that everything is on their own

It is not a good thing to do, time and effort not to say that may be doing a useless work, but their own research and study

It's still a necessity. Not to mention the third-party framework I used called Xlistview. This is its managed address:

Https://github.com/Maxwin-z/XListView-Android

After downloading it down

Copy the extracted content to your own project (you can build a package to store it, I did it)

Note that layout,string and drawable also have several corresponding files, which also need to go into the examination.

Then write the layout for the ListView

Note: <com.xxxxxxx the location of your own corresponding file xxx>

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical"Android:background= "#f0f0f0">    <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello" />    <Com.example.guwie.yaoren.ui.view.XListViewAndroid:id= "@+id/xlistview"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:cachecolorhint= "#00000000">    </Com.example.guwie.yaoren.ui.view.XListView></LinearLayout>


Write the corresponding Java file for the layout file

 Public classTaskListextendsAppcompatactivityImplementsXlistview.ixlistviewlistener {//Create a Xlistview object    PrivateXlistview Mlistview; //Create DataList<itembean> dataList =NewArraylist<>(); PrivateObject Item; intpage = 1; intMaxpage = 0; //used to perform time-consuming operations    PrivateHandler Mhandler; /*** Called when the activity is first created. */@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.tasklist); //Xlistview controls in the specified layoutMlistview =(Xlistview) Findviewbyid (R.id.xlistview); Mlistview.setpullloadenable (true); Mlistview.setpullrefreshenable (true);        Gettasklist (page); Mlistview.setadapter (NewMyadapter (TaskList. This, dataList)); Mlistview.setxlistviewlistener ( This); Mhandler=NewHandler () {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { Case0x001: Toast.maketext (TaskList. This, "No More Data", Toast.length_short). Show ();  Break;  Case0x002: Toast.maketext (TaskList. This, "Data not Found", Toast.length_short). Show ();  Break;    }            }        }; }    //the OnLoad () method is executed after the data has been obtained, otherwise the refresh will never stop.    Private voidonLoad () {Mlistview.stoprefresh ();        Mlistview.stoploadmore (); Mlistview.setrefreshtime (Just); } @Override Public voidOnrefresh () {datalist.clear (); Page= 1; Mhandler.post (NewRunnable () {//Perform time-consuming operation @Override Public voidrun () {gettasklist (page); Mlistview.setadapter (NewMyadapter (TaskList. This, dataList));        }        });        OnLoad (); LOG.I ("------------------", "Onrefresh"); } @Override Public voidOnloadmore () {Mhandler.post (NewRunnable () {@Override Public voidrun () {if(Page <maxpage) {Gettasklist (++page);                Mlistview.defernotifydatasetchanged (); }Else{Message msg=NewMessage (); Msg.what=0x001;                Mhandler.sendmessage (msg); }//gettasklist (++page);//mlistview.defernotifydatasetchanged (); //Mlistview.setadapter (New Myadapter (Tasklist.this, dataList));            }        });        OnLoad (); LOG.I ("------------------", "Onloadmore"); }    /*** * Get list data volley method post string data get JSON data service driven by PHP write * **/     Public voidGettasklist (intpages) {Toast.maketext (TaskList). This, pages+ "", Toast.length_short). Show (); Requestqueue Mreuestqueut= Volley.newrequestqueue ( This); FinalGson Gson =NewGson (); Jsonobjectrequest Jsreq=NewJsonobjectrequest (Request.Method.GET, task_list_date+ "page=" +pages,NULL,                NewResponse.listener<jsonobject>() {@Override Public voidonresponse (Jsonobject jsonobject) {Message msg=NewMessage (); Try {                            intCode = jsonobject.getint ("code"); if(Code = = 200){                                intSize = Jsonobject.getjsonarray ("Data"). Length ();  for(inti=0; I<size; i++) {Jsonobject dataObject= Jsonobject.getjsonarray ("Data"). Getjsonobject (i); String title= Dataobject.getstring ("title"); String Price= dataobject.getstring ("Fee"); String explain= dataobject.getstring ("description"); Datalist.add (NewItembean (title, explain, Price, "3km"))); } maxpage= ++page; Page= Page-1; }Else if(Code = = 205){                                //no more data already//maxpage = page;page =Maxpage; Msg.what=0x001;                            Mhandler.sendmessage (msg); }Else if(Code = = 404){                                //No data foundMsg.what = 0x002;                            Mhandler.sendmessage (msg); }                        } Catch(jsonexception e) {e.printstacktrace (); LOG.I ("----------------", "Client Internal error"); }                    }                },                NewResponse.errorlistener () {@Override Public voidonerrorresponse (Volleyerror volleyerror) {log.i ("----------------", "Server error" +volleyerror.tostring ());        }                });    Mreuestqueut.add (Jsreq); }}

The method of Myadapter here is custom, please refer to Xu Dashen's "Android must learn-baseadapter use and optimization" for details.
The above method is about the use of volley please Baidu Operation effect:

demo:http://www.thinksaas.cn/group/topic/348557/of the Learning Place

Thanks to the author

Android third-party framework Xlistview use

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.