Android_listview loading More (ListView paging feature)

Source: Internet
Author: User

First on

The data is finished loading

First define a layout that is loading at the bottom footer_layout.xml

<?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 "&    Gt <linearlayout android:id= "@+id/load_layout" android:layout_width= "Match_parent" Android:layout_hei ght= "wrap_content" android:gravity= "center" android:orientation= "Horizontal" android:paddingbottom= "1             0dip "android:paddingtop=" 10dip "> <progressbar style="? Android:attr/progressbarstylesmall "            Android:layout_width= "Wrap_content" android:layout_height= "wrap_content"/> <textview Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:textsize = "16sp" android:textcolor= "@color/red" android:textstyle= "bold" android:text= "Loading ..."/&    Gt </LinearLayout></LinearLayout> 

Customizing the ListView---------Loadlistview.java

Package Com.zwb.listviewloaddata.view;import Android.content.context;import Android.util.attributeset;import Android.view.layoutinflater;import Android.view.view;import Android.widget.abslistview;import Android.widget.listview;import com.zwb.listviewloaddata.r;/** * Class Description: Custom ListView * ZWB * Time: 16-5-9 11:21 * Email: @163.com * * public class Loadlistview extends ListView implements Abslistview.onscrolllistener {View footer;//bottom layout; int Totali    Total number of temcount;//; int lastvisibleitem;//last visible item; boolean isloading;//loading; Iloadlistener iloadlistener;        Public Loadlistview (Context context) {super (context);    Initview (context);        } public Loadlistview (context context, AttributeSet Attrs) {Super (context, attrs);    Initview (context);        } public Loadlistview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);    Initview (context);    }/** * Add bottom load hint layout to ListView * * @param context */private void Initview (context context) {Layoutinflater Inflater = layoutinflater.from (context);        Footer = inflater.inflate (r.layout.footer_layout, NULL);        Footer.findviewbyid (r.id.load_layout). setvisibility (View.gone);        This.addfooterview (footer);    This.setonscrolllistener (this); } @Override public void onscroll (Abslistview view, int firstvisibleitem, int Visibleitemcoun T, int totalitemcount) {//TODO auto-generated method Stub this.lastvisibleitem = Firstvisibleitem + Visibl        Eitemcount;    This.totalitemcount = Totalitemcount; } @Override public void onscrollstatechanged (Abslistview view, int scrollstate) {//TODO auto-generated Meth            OD stub if (totalitemcount = = Lastvisibleitem && scrollstate = = Scroll_state_idle) {                if (!isloading) {isloading = true;              Footer.findviewbyid (r.id.load_layout). Setvisibility (          view.visible);            Load more iloadlistener.onload ();        }}}/** * Load complete */public void LoadComplete () {isloading = false;    Footer.findviewbyid (r.id.load_layout). setvisibility (View.gone);    } public void Setinterface (Iloadlistener iloadlistener) {this.iloadlistener = Iloadlistener;    }//callback interface for loading more data public interface iloadlistener{public void OnLoad (); }}

Referencing a custom listview in a layout file

Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    xmlns:tools=" Http://schemas.android.com/tools "    android:layout_width=" Match_parent    " android:background= "@color/white"    android:layout_height= "match_parent"    tools:context= " Com.zwb.listviewloaddata.MainActivity ">    <com.zwb.listviewloaddata.view.loadlistview        android:id=" @+id/listview "        android:layout_width=" match_parent "        android:layout_height=" Wrap_content "        android: background= "@color/white"        android:cachecolorhint= "#00000000"        android:dividerheight= "5dip"        /> </RelativeLayout>

The item resource file for the ListView:

Item_layout.xml

<?xml version= "1.0" encoding= "Utf-8"? ><android.support.v7.widget.cardview xmlns:android= "/http Schemas.android.com/apk/res/android "xmlns:card_view=" Http://schemas.android.com/apk/res-auto "Android:layout_ Width= "Match_parent" android:layout_height= "wrap_content" android:layout_marginleft= "5DP" android:layout_marginRi ght= "5DP" android:layout_centerinparent= "true" android:background= "@color/white" card_view:cardcornerradius= "8DP" > <linearlayout android:gravity= "center_vertical" android:orientation= "Horizontal" android:b ackground= "@color/white" android:layout_width= "match_parent" android:layout_height= "60DP" > <ima Geview android:id= "@+id/item_img" android:layout_width= "50DP" android:layout_height= "50DP "Android:background=" @mipmap/ic_launcher "android:layout_marginleft=" 10DP "/> <linearla Yout android:layout_marginleft= "10DP"Android:orientation=" vertical "android:layout_width=" wrap_content "Android:layout_heigh                 t= "Wrap_content" > <textview android:id= "@+id/item_name" android:text= "I am the title" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/> & Lt TextView android:id= "@+id/item_msg" android:text= "The weather is fine today" android:layout_wi Dth= "Wrap_content" android:layout_height= "wrap_content"/> </LinearLayout> </linearla Yout></android.support.v7.widget.cardview>

The next step is to initialize the data in the activity and set the data to the ListView

Mainactivity.java

Package Com.zwb.listviewloaddata;import Android.os.handler;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.widget.listview;import Com.zwb.listviewloaddata.adapter.itemadapter;import Com.zwb.listviewloaddata.entity.itementity;import Com.zwb.listviewloaddata.view.loadlistview;import Java.util.arraylist;import Java.util.list;public class Mainactivity extends Appcompatactivity implements    loadlistview.iloadlistener{private Arraylist<itementity> list = new arraylist<> ();    Private Loadlistview ListView;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main);        Method of initializing data getData ();    Showlistview (list);    } private Itemadapter adapter; private void Showlistview (arraylist<itementity> list) {if (adapter = = NULL) {ListView = (loadlis            TView) Findviewbyid (R.id.listview));Listview.setinterface (this);            adapter = new Itemadapter (Getapplicationcontext (), list);        Listview.setadapter (adapter);        }else {adapter.ondatechange (list); }} private void Getmoredata () {for (int i = 0; i < 3; i++) {itementity entity = new Itementi            Ty ();            Entity.setname ("New test title");            Entity.setmsg ("I am a new test content ah ~");        List.add (entity); }} private void GetData () {for (int i = 0; i < i++) {itementity entity = new Itementity (            );            Entity.setname ("test title");            Entity.setmsg ("I am a test of content ah ~");        List.add (entity);        }} @Override public void OnLoad () {Handler Handler = new Handler ();                Handler.postdelayed (New Runnable () {@Override public void run () {//Load data                Getmoredata ();                Update the ListView display Showlistview (list); LiStview loading completed listview.loadcomplete ();    }}, 2000); }}

Adapter of the ListView

Itemadapter.java

Package Com.zwb.listviewloaddata.adapter;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.TextView; Import Com.zwb.listviewloaddata.r;import Com.zwb.listviewloaddata.entity.itementity;import java.util.ArrayList; Import java.util.list;/** * Class Description: ListView Adapter * ZWB * Time: 16-5-9 11:06 * Email: @163.com */public class Itemadapter extends    baseadapter{private context context;    Private arraylist<itementity> list;        Public Itemadapter (Context context, arraylist<itementity> list) {This.context = context;    This.list = list;        } public void Ondatechange (arraylist<itementity> list) {this.list = list;    This.notifydatasetchanged ();    } @Override public int getcount () {return list.size ();    } @Override public Object getItem (int position) {return list.get (position); } @Override PublicLong Getitemid (int position) {return position; } @Override public View getView (int position, view Convertview, ViewGroup parent) {itementity entity = list.        Get (position);        Viewholder Holder;            if (Convertview = = null) {holder = new Viewholder ();            Convertview = Layoutinflater.from (context). Inflate (R.layout.item_layout, parent, false);            Holder.name = ((TextView) Convertview.findviewbyid (r.id.item_name));            Holder.msg = ((TextView) Convertview.findviewbyid (r.id.item_msg));        Convertview.settag (holder);        }else {holder = ((Viewholder) Convertview.gettag ());        } holder.name.setText (Entity.getname ());        Holder.msg.setText (Entity.getmsg ());    return convertview;        } class viewholder{TextView name;    TextView msg; }}

OK is done.

Download link

Android_listview Sub-page

Android_listview loading More (ListView paging feature)

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.