. NET programmer play to Android Development---() ListView Paging Event

Source: Internet
Author: User

The ListView when loading is data, if all the data is loaded out at once, so if the data volume is large, inefficient, poor performance, usually the measure is to page load, only load the current number of pages of data. This section shows you how to load data in the ListView page. First Look at the


1. Create a Load progress bar

Each time the ListView loads, it will have a loading progress bar at the bottom, display loading, we create a layout file like this, the code is as follows

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <linearlayout android:layout_width= "match_parent" android:layout_height= "Match_parent" Android:or            ientation= "vertical" android:gravity= "center" android:paddingtop= "10dip" > <progressbar Android:id= "@+id/progressbar1" style= "? Android:attr/progressbarstylelarge" Android:layout_width= "W Rap_content "android:layout_height=" wrap_content "/> <textview android:id=" @+id/textvie W1 "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "Android:text = "Loading ..."/> </LinearLayout> </LinearLayout>

2. Customizing the ListView

Above we created a load progress layout file, and then we want to add this layout file to the ListView, now we create a custom Listviewpager, the custom Listviewpager inherits Listviiew, With the Addfooterview method, the load file is added to the ListView, and the data is loaded by listening to the scroll events of the ListView, so the Onscrolllistener interface is inherited.

Package Com.example.helloword;import Android. R.bool;import Android.content.context;import Android.os.handler;import Android.util.attributeset;import Android.view.layoutinflater;import Android.view.view;import Android.widget.abslistview;import android.widget.listview;//custom Listviewpublic Class Listviewpager extends ListView implements Android.widget.AbsListView.OnScrollListener {View footer;//defines the bottom viewint lastitem;//the last visible number int totalitem;// The total number of Boolean isload;//is loading idatainterface datainter;public Listviewpager (context context) {super (context); Nitfooter (context);//TODO auto-generated constructor stub} public Listviewpager (context context, AttributeSet attrs)        {Super (context, attrs);    Initfooter (context);        } public Listviewpager (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);    Initfooter (context); }//Initialize the bottom layout bar private void Initfooter (Context oontext) {layoutinflater inflater=layoutinflater.from (oontext);//Add bottom bar FOoter=inflater.inflate (r.layout.listviewfooter, null); footer.setvisibility (view.gone);// When initializing, set the bottom invisible This.addfooterview (footer); This.setonscrolllistener (this);} @Overridepublic void Onscroll (Abslistview arg0, int arg1, int arg2, int arg3) {//TODO auto-generated method Stub//arg1 Visible The first number arg2 visible quantity arg3 The total quantity this.lastitem=arg1+arg2;//the last visible number equals the current first visible number plus the visible number THIS.TOTALITEM=ARG3;} Load complete @overridepublic void onscrollstatechanged (Abslistview arg0, int arg1) {//TODO auto-generated method stub scroll state equals scroll stop Stop, the parameter arg1 indicates the scroll state if (lastitem==totalitem&&arg1==scroll_state_idle) {///Last and the total equality description has been to the bottom of the ListView if (!isload) { Isload=true;footer.setvisibility (view.visible);//Set Visible handler Handler=new handler (); Handler.postdelayed (new Runnable () {public void Run () {}},;d Atainter). LoadData ();//Load Data isload=false;//footer.setvisibility (view.gone);//Set Visible}}}public void Setinterface (idatainterface data) {Datainter=data;}}

Onscroll (Abslistview arg0, int arg1, int arg2, int arg3) have three important parameters

Arg1 represents the first number currently visible

ARG2 indicates the number of visible

ARG3 indicates the total quantity

Onscrollstatechanged (Abslistview arg0, int arg1) indicates a rolling state change

If the scrolling state ends, the data is loaded.

3. Customizing the ListView binding Data

After customizing the ListView creation, we create a demo to bind the data, first creating a layout file and an activity file

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >             <com.example.helloword.listviewpager        android:id= "@+id/lvpager"        Android:layout_width= "Match_parent"        android:layout_height= "wrap_content" >    </ Com.example.helloword.listviewpager>         </LinearLayout>

Package Com.example.helloword;import Java.util.arraylist;import Java.util.hashmap;import java.util.List;import Java.util.map;import Android.app.activity;import Android.os.bundle;import Android.widget.listview;import Android.widget.simpleadapter;public class Pagelistview extends Activity implements Idatainterface {private      Listviewpager LV; Simpleadapter adp;//defines the adapter private list<map<string,object>> maplist;//defines the data source protected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.pagerlistview); lv= ( Listviewpager) Findviewbyid (R.id.lvpager); Lv.setinterface (this); Maplist=new Arraylist<map<string,object      >> ();      for (int i=0;i<6;i++) {map<string,object> map=new hashmap<string,object> ();      Map.put ("Code", "Code: +i");     Map.put ("name", "title: Ipad" +i);     Map.put ("Price", "Prices:" +i);      Map.put ("Model", "unit:" +i);      Maplist.add (map); } adp=new Simpleadapter (PagelistvieW.this, Maplist,r.layout.listdetail, new string[]{"code", "name", "Price", "model"}, new Int[]{r.id.tvcode,r.id.tvname      , R.id.tvprice,r.id.tvmodel});              Lv.setadapter (ADP); } @Overridepublic void LoadData () {//TODO auto-generated method stubfor (int i=10;i<16;i++) {Map<string,ob      Ject> map=new hashmap<string,object> ();      Map.put ("Code", "Code: +i");     Map.put ("name", "title: Ipad" +i);     Map.put ("Price", "Prices:" +i);      Map.put ("Model", "unit:" +i);      Maplist.add (map); }adp.notifydatasetchanged ();}}


Data is passed to a custom ListView through an interface in Pagelistview, so we're going to create an interface file
Package Com.example.helloword;public interface Idatainterface {public void LoadData ();//Load Data Interface}

Demo Download: http://download.csdn.net/detail/zx13525079024/8316547

. NET programmer play to Android Development---() ListView Paging Event

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.