In Android application development, the use of ListView components to show the data is a very common function, when an application to show a lot of data, usually will not put all the data on the display at once, but through the form of pagination to show the data, personally feel that this will have a better user experience. Therefore, many applications use the form of batch loading to get the data that the user needs. For example, a microblog client may automatically load the next page of data when the user slides to the bottom of the list, or it may place a "view more" button at the bottom, and then load the next page of data after the user clicks.
Here's a demo to show how the ListView feature is implemented : the demo loads the news (analog news client) paging data by adding a "view more ..." button at the bottom of the ListView list. At the same time limit 10 records per load, but after the full load of data, the ListView list at the bottom of the view "view more ..." delete. Assume that the total number of data loaded is 38 records. Take a look at the program structure diagram of the demo project:
The News.java class in package Com.andyidea.bean is the news entity class, and the Paginationlistviewactivity.java class in package Com.andyidea.listview is used to show the ListView list. The layout layout contains three layout files, namely: List_item.xml, Loadmore.xml, Main.xml. Below the following separate source:
Layout in the List_item.xml Source:
<span style= "FONT-SIZE:13PX;" ><?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://" Schemas.android.com/apk/res/android "
android:layout_width=" fill_parent "
android:layout_height=" Fill_ Parent "
android:orientation=" vertical ">
<textview
android:id=" @+id/newstitle "
android: Layout_width= "Fill_parent"
android:layout_height= "wrap_content"/> <textview android:id=
"@+" Id/newscontent "
android:layout_width=" fill_parent "
android:layout_height=" wrap_content "/>
" </LinearLayout></span>
Layout Loadmore.xml Source:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/" Apk/res/android "
android:layout_width=" fill_parent "
android:layout_height=" fill_parent ">
< Button
android:id= "@+id/loadmorebutton"
android:layout_width= "fill_parent"
android:layout_height= " Wrap_content "
android:text=" view More ... "/>
</LinearLayout>
Layout Main.xml Source:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" fill_parent "
android:layout_" height= "Fill_parent" >
<listview
android:id= "@+id/lvnews" android:layout_width= "Fill_"
Parent "
android:layout_height=" wrap_content "/>
</linearlayou
Package Com.andyidea.bean in the News.java class Source:
Package Com.andyidea.bean;
public class News {
private String title; Title
private string content;//Content public
string GetTitle () {return
title;
}
public void Settitle (String title) {
this.title = title;
}
Public String getcontent () {return
content;
}
public void SetContent (String content) {
this.content = content;
}
}
Package Com.andyidea.listview in the Paginationlistviewactivity.java class Source:
Package Com.andyidea.listview;
Import java.util.ArrayList;
Import java.util.List;
Import Com.andyidea.bean.News;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.AbsListView;
Import Android.widget.AbsListView.OnScrollListener;
Import Android.widget.BaseAdapter;
Import Android.widget.Button;
Import Android.widget.ListView;
Import Android.widget.TextView;
Import Android.widget.Toast;
public class Paginationlistviewactivity extends activity implements Onscrolllistener {private ListView ListView; private int visiblelastindex = 0; The final visual index is private int visibleitemcount; Total number of current window visible items private int datasize = 38;
The number of bars of the analog dataset private Paginationadapter adapter;
Private View Loadmoreview;
Private Button Loadmorebutton;
Private Handler Handler = new Handler (); /** called the activity is the created.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Loadmoreview = Getlayoutinflater (). Inflate (R.layout.loadmore, NULL);
Loadmorebutton = (Button) Loadmoreview.findviewbyid (R.id.loadmorebutton); Loadmorebutton.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {Loadmorebutton.settext ("Loading ...");
Set Button text handler.postdelayed (new Runnable () {@Override public void run () {
Loadmoredata ();
Adapter.notifydatasetchanged (); Loadmorebutton.settext ("View More ...");
Restore button text}},2000);
}
});
ListView = (ListView) Findviewbyid (r.id.lvnews); Listview.addfooterview (Loadmoreview);
Set the bottom view of the list initializeadapter ();
Listview.setadapter (adapter); Listview.setOnscrolllistener (this); @Override public void onscrollstatechanged (Abslistview view, int scrollstate) {int itemslastindex = ADAP Ter.getcount ()-1;
The index of the last item in the dataset int lastindex = Itemslastindex + 1; if (scrollstate = = Onscrolllistener.scroll_state_idle && Visiblelastindex = = lastindex) {//if automatic Loading, where you can place the code that asynchronously loads the data} @Override public void onscroll (Abslistview view, int firstvisibleitem,
T visibleitemcount, int totalitemcount) {this.visibleitemcount = VisibleItemCount;
Visiblelastindex = Firstvisibleitem + visibleItemCount-1;
LOG.E ("=========================", "========================");
LOG.E ("Firstvisibleitem =", firstvisibleitem+ "");
LOG.E ("VisibleItemCount =", visibleitemcount+ "");
LOG.E ("Totalitemcount =", totalitemcount+ "");
LOG.E ("=========================", "========================"); If all of the record options are equal to the number of bars in the dataset, remove the bottom view of the list if (Totalitemcount = Datasize+1) {Listview.removefooterview (Loadmoreview);
Toast.maketext (This, "all data Loaded!", Toast.length_long). Show (); }/** * Initialize ListView adapter */private void Initializeadapter () {list<news> News = new Array
List<news> ();
for (int i=1;i<=10;i++) {News items = new News ();
Items.settitle ("Title" +i);
Items.setcontent ("This is News Content" +i);
News.add (items);
} adapter = new Paginationadapter (news);
/** * Load More data * * private void Loadmoredata () {int count = Adapter.getcount ();
if (count+10 <= datasize) {for (int i=count+1; i<=count+10; i++) {News item = new News ();
Item.settitle ("Title" +i);
Item.setcontent ("This is News Content" +i);
Adapter.addnewsitem (item);
}}else{for (int i=count+1; i<=datasize; i++) {News item = new News ();
Item.settitle ("Title" +i); Item.setcOntent ("This is News Content" +i);
Adapter.addnewsitem (item);
}} class Paginationadapter extends baseadapter{list<news> newsitems;
Public Paginationadapter (list<news> newsitems) {this.newsitems = Newsitems;
@Override public int GetCount () {return newsitems.size ();
@Override public Object getitem (int position) {return newsitems.get (position);
@Override public long getitemid (int position) {return position;
@Override public View getview (int position, view view, ViewGroup parent) {if (view = = null) {
view = Getlayoutinflater (). Inflate (R.layout.list_item, NULL);
}//News title TextView Tvtitle = (TextView) View.findviewbyid (r.id.newstitle);
Tvtitle.settext (Newsitems.get (position). GetTitle ()); News content TextView Tvcontent = (TextView) View.findviewbyid (r.id. newscontent);
Tvcontent.settext (Newsitems.get (position). GetContent ());
return view; /** * Add Data list item * @param newsitem */public void Addnewsitem (news NewsItem) {News
Items.Add (NewsItem);
}
}
}
Finally, the results of the run program screenshot are as follows:
Through the screenshot above, when we click the "View More ..." button, the next 10 records are loaded, and when all the records are loaded, the bottom view of the ListView will be removed.
I hope this article will help you learn about Android software programming.