On a copy of the Web client's drawer mode, this article continues, to write a write load more This feature, through the custom implementation load more, first:
Today's implementation is the bottom of the 20 loading ... This function!
Let's take a look at the idea:
1. Add 20 loads of this layout to the ListView and hide
2. Join Onscrolllistener Monitor, by listening to scrolling events, when scrolling to the lowest end, display the above layout
3. Load more functions via interface callback
4. When the data is loaded, notify the ListView that the load is finished and hide the layout file above
Directly below the code:
1. Add 20 loads of this layout to the ListView and hide
Layoutinflater Inflater = layoutinflater.from (context); Footview = Inflater.inflate (r.layout.foot_layout, NULL); Addfooterview (Footview); Footview.findviewbyid (r.id.foot_layout). setvisibility (View.gone);
2. Join Onscrolllistener Monitor, by listening to scrolling events, when scrolling to the lowest end, display the above layout
@Overridepublic void onscrollstatechanged (abslistview view, int scrollstate) {if (LastItem = = Totalitemcount && s Crollstate = = Scroll_state_idle) {if (!isloading) {Isloading=true;footview.findviewbyid (r.id.foot_layout). Setvisibility (view.visible); Isloadinglistener.onload ();}} @Overridepublic void Onscroll (abslistview view, int firstvisibleitem,int visibleitemcount, int totalitemcount) { LastItem = firstvisibleitem + Visibleitemcount;this.totalitemcount = totalitemcount;}
3. Load more functions via interface callback
public void Setonloadinglistener (Isloadinglistener isloadinglistener) {This.isloadinglistener=isloadinglistener;} public interface isloadinglistener{public void OnLoad ();
@Overridepublic void OnLoad () {handler.postdelayed (new Runnable () {@Overridepublic void run () {List.add ("Papa"); List.add ("Mother"); List.add ("I"); adapter.notifydatasetchanged (); Listview.complateload ();}, 3000);}
4. When the data is loaded, notify the ListView that the load is finished and hide the layout file above
public void Complateload () {Isloading=false;footview.findviewbyid (r.id.foot_layout). setvisibility (View.GONE);}
OK, that's what the custom control is. Here's the complete code
Package Com.sdufe.thea.guo.view;import Com.sdufe.thea.guo.r;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 Android.widget.AbsListView.OnScrollListener; public class Listviewloadmore extends ListView implements Onscrolllistener {View footview;int lastitem;//Last int totalit Emcount; How many Boolean Isloading=false;isloadinglistener isloadinglistener;public Listviewloadmore are there at the moment (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Initview (context);} Public Listviewloadmore (context context, AttributeSet Attrs) {Super (context, attrs); Initview (context);} Public Listviewloadmore (Context context) {super (context); Initview (context);} /** * Initialize Footview * * @param context */void Initview (context context) {Layoutinflater inflater = Layoutinflater.from (cont EXT); Footview = Inflater.inflate (r.layout.foot_layout, null); AddfooterView (Footview); Footview.findviewbyid (r.id.foot_layout) setvisibility (View.gone); Setonscrolllistener (this);} @Overridepublic void onscrollstatechanged (abslistview view, int scrollstate) {if (LastItem = = Totalitemcount && s Crollstate = = Scroll_state_idle) {if (!isloading) {Isloading=true;footview.findviewbyid (r.id.foot_layout). Setvisibility (view.visible); Isloadinglistener.onload ();}} @Overridepublic void Onscroll (abslistview view, int firstvisibleitem,int visibleitemcount, int totalitemcount) { LastItem = firstvisibleitem + Visibleitemcount;this.totalitemcount = totalitemcount;} public void Setonloadinglistener (Isloadinglistener isloadinglistener) {This.isloadinglistener=isloadinglistener;} public interface isloadinglistener{public void OnLoad (); public void Complateload () {Isloading=false;footview.findviewbyid (r.id.foot_layout). setvisibility (View.GONE);}}
Main interface layout:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " > <com.sdufe.thea.guo.view.listviewloadmore android:id= "@+id/listview" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:divider= "@null"/></linearlayout>
Main interface Code:
Package Com.sdufe.thea.guo;import Java.util.arraylist;import Java.util.list;import Com.sdufe.thea.guo.view.listviewloadmore;import Com.sdufe.thea.guo.view.ListViewLoadMore.IsLoadingListener; Import Android.os.bundle;import android.os.handler;import Android.app.activity;import Android.view.Menu;import Android.widget.arrayadapter;import Android.widget.listview;public class Mainactivity extends Activity implements Isloadinglistener{private listviewloadmore listview;private list<string> list;private ArrayAdapter<String > Adapter;private Handler handler=new Handler (); @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); ListView = (Listviewloadmore) Findviewbyid (R.id.listview); InitData (); adapter = new Arrayadapter<string> (this,android. R.layout.simple_list_item_1, list); Listview.setadapter (adapter); Listview.setonloadinglistener (this);} /** * Initialize list value */private void InitData () {list = new ArrayList<String> (), List.add ("123456789"), List.add ("123456789"), List.add ("123456789"), List.add ("123456789"); List.add ("123456789"), List.add ("123456789"), List.add ("123456789"), List.add ("123456789"), List.add ("123456789"); List.add ("123456789"), List.add ("123456789"), List.add ("123456789"), List.add ("123456789"), List.add ("123456789"); List.add ("123456789"); List.add ("123456789"); List.add ("123456789");} @Overridepublic void OnLoad () {handler.postdelayed (new Runnable () {@Overridepublic void run () {List.add ("Papa"); List.add ("Mother"); List.add ("I"); adapter.notifydatasetchanged (); Listview.complateload ();}, 3000);}}
Do not understand the message, try to answer you!
CSND Code: http://download.csdn.net/detail/elinavampire/8204105
Github:https://github.com/zimoguo/pulltorefreshloadmore
Android custom Controls (iv) Imitation NetEase client pull-up load more