Listview drop-down load 1 (paging)

Source: Internet
Author: User

First, create a listview list in the main XML file. The Code is as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    tools:context=".MainActivity" >    <ListView        android:id="@+id/menuList"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_weight="1" >    </ListView></LinearLayout>

 

Create two more XML files, including the list item (menu. XML) and the load interface (loadmore. XML)

The menu. XML 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" >        <TextView        android:id="@+id/showView"        android:layout_width="fill_parent"         android:layout_height="wrap_content"        android:padding="10dp"        style="@style/normalText"        android:text="title"        />    <TextView         android:id="@+id/content"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:padding="10dp"        android:text="123"        /></LinearLayout>

 

Loadmore. xml

<? 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 = "horizontal"> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: gravity = "center" Android: Background = "@ drawable/list_bg"> <progressbar Android: id = "@ + ID/progressbar1" Android: layout_width = "25dp" Android: layout_height = "25dp"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "loading data... "/> </linearlayout>

 

Loadmore background style list_bg.xml:

<?xml version="1.0" encoding="utf-8"?>  <shape xmlns:android="http://schemas.android.com/apk/res/android">        <gradient             android:startColor="#ccc"             android:endColor="#ccc"             android:angle="45"        />  </shape>  

 

 

The entry code is as follows:

Package COM. TP. soft. APP; import Java. util. arraylist; import Java. util. list; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. util. log; import android. view. layoutinflater; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. view. viewgroup; import android. widget. abslistview; import android. widget. abslistview. onscrolllistener; import android. widget. baseadapter; import android. widget. listview; import android. widget. progressbar; import android. widget. textview; public class mainactivity extends activity {private listview mlistview; private view mloadmoreview; private progressbar mloadbtn; private pageadapter adapter; private handler = new handler (); @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); mloadmoreview = getlayoutinflater (). inflate (R. layout. loadmore, null); mloadbtn = (progressbar) mloadmoreview. findviewbyid (R. id. progressbar1); // mloadbtn. setonclicklistener (this); mlistview = (listview) findviewbyid (R. id. menulist); // scroll down to trigger the event mlistview. setonscrolllistener (New onscrolllistener () {@ override public void onscrollstatechanged (abslistview view, int scrollstate) {// If (scrollstate = onscrolllistener. scroll_state_idle) {// scroll to the bottom if (view. getlastvisibleposition () = view. getcount ()-1) {// mloadbtn. settext ("loading... "); myrunnable r = new myrunnable (); handler. postdelayed (R, 2000) ;}}@ override public void onscroll (abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {}}); mlistview. addfooterview (mloadmoreview); List <string> itemlist = new arraylist <string> (); For (INT I = 0; I <10; I ++) {itemlist. add ("title" + I);} adapter = new pageadapter (itemlist); mlistview. setadapter (adapter) ;}@ override public Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. main, menu); Return true;}/* @ override public void onclick (view v) {mloadbtn. settext ("loading... "); myrunnable r = new myrunnable (); handler. postdelayed (R, 2000);} */class myrunnable implements runnable {@ override public void run () {log. E ("prompt", "123"); loadmoredate (); // update the UI adapter. notifydatasetchanged (); // mloadbtn. settext ("view more... ") ;}} class pageadapter extends baseadapter {list <string> itemlist; Public pageadapter (list <string> itemlist) {This. itemlist = itemlist;} @ override public view getview (INT position, view convertview, viewgroup parent) {If (convertview = NULL) {convertview = layoutinflater. from (getapplicationcontext ()). inflate (R. layout. menu, null);} textview titleview = (textview) convertview. findviewbyid (R. id. showview); titleview. settext (itemlist. get (position); Return convertview;} @ override public long getitemid (INT position) {return position ;}@ override public object getitem (INT position) {return itemlist. get (position) ;}@ override public int getcount () {return itemlist. size ();} public void additem (string Str) {itemlist. add (STR) ;}} private void loadmoredate () {log. E ("Total", "" + adapter. getcount (); int COUNT = adapter. getcount (); For (INT I = count + 1; I <count + 10; I ++) {adapter. additem ("title" + I );}}}

 

Running result:

Listview drop-down load 1 (paging)

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.