Latest Android ListView drop-down refresh on slide load

Source: Internet
Author: User

In the development of the project, the basic will be used in the ListView drop-down and up-loading more, before the most commonly used should be pulled to refresh or its variant version of it, Google official added a new class swiperefreshlayout in the latest ANDROID.SUPPORT.V4 package, the role of this class is to provide the official drop-down refresh, and the effect is quite good, While the pull-up loads more then using our custom ListView, is also quite simple.

Drop-down Refresh


The following is a simple introduction:

First it is a viewgroup, but it is only allowed to have a child control, the child control can be any view, when used, the class implements the Onrefreshlistener interface, in the Onrefresh () method to achieve the task to be completed,

Findviewbyid after getting swiperefreshlayout, show refresh animation with swiperefreshlayout.setrefreshing (true); Canceling the Refresh animation with swiperefreshlayout.setrefreshing (false) is fairly straightforward.

In addition, there are some other common methods such as judging when the refresh animation is displayed, with Swiperefreshlayout.isshown ().

Layout:

<android.support.v4.widget.swiperefreshlayout        android:id= "@+id/swip_index"        android:layout_width= " Match_parent "        android:layout_height=" match_parent ">        < Com.example.listviewloadmore.LoadMoreListView            android:id= "@+id/listview"            android:layout_width= "Wrap_ Content "            android:layout_height=" wrap_content "            android:text=" @string/hello_world ">        </ Com.example.listviewloadmore.loadmorelistview>    </android.support.v4.widget.SwipeRefreshLayout>


Pull-up load more ideas: Listen to the sliding events and determine whether to reach the bottom, when reaching the bottom, callback we defined a method, so as to achieve pull-up load more purposes. But specifically how to listen and callback, we might as well look at the button control Click event Execution Flow, Button inherits TextView, TextView inherit View1. Implement the Onclicklistener interface, overriding the OnClick Method 2. Setting the Button.setonclicklistener (this) or the new interface is the same.
Check the view source, we can learn
The view class has a Onclickoistener interface, and the Setonclicklistener method

So the steps we're going to implement in our own ListView are very clear about the 1.myListView inheritance listView2. Implementing the Onloadmore Interface 3. Declare the Onloadmore interface object in the Class 4. Set the click Method SetOnLoadMore5. Call on in the desired place Loadmore.loadmore method. The following is attached Mylistview source code:
Package Com.example.listviewloadmore;import Android.annotation.suppresslint;import Android.content.context;import Android.util.attributeset;import Android.util.log;import Android.view.layoutinflater;import Android.view.View; Import Android.widget.abslistview;import Android.widget.abslistview.onscrolllistener;import Android.widget.listview;public class Loadmorelistview extends ListView implements onscrolllistener{private View footer ;p rivate int Totalitem;private int lastitem;private boolean isloading;private onloadmore onloadmore;private Layoutinflater Inflater;public Loadmorelistview (context context) {super (context); init (context);} Public Loadmorelistview (context context, AttributeSet Attrs) {Super (context, attrs); init (context);} Public Loadmorelistview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); Init ( context);} @SuppressLint ("Inflateparams") private void init (context context) {Inflater = Layoutinflater.from (context); footer = Inflater.inflate (r.layout. Load_more_footer,null, False); footer.setvisibility (View.gone); This.addfooterview (footer); This.setonscrolllistener (this);} @Overridepublic void Onscroll (abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { This.lastitem = Firstvisibleitem+visibleitemcount;this.totalitem = Totalitemcount;} @Overridepublic void onscrollstatechanged (abslistview view, int scrollstate) {if (This.totalitem = = lastitem&& Scrollstate = = Scroll_state_idle) {log.v ("isloading", "yes"), if (!isloading) {isloading=true;footer.setvisibility ( view.visible); Onloadmore.loadmore ();}}} public void Setloadmorelisten (Onloadmore onloadmore) {this.onloadmore = Onloadmore;} /** * Load Complete Call this method */public void Onloadcomplete () {footer.setvisibility (view.gone); isloading = false;} public interface onloadmore{public void Loadmore ();}}


The code is not very complicated, you can see it carefully. The bottom is loading the layout code
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:id=" @+id/load_more_footer "android:layout_width=" fill_parent "android:layout_height=" Wrap_conten        T "> <linearlayout android:layout_width=" wrap_content "android:layout_height=" Wrap_content "            Android:layout_centerinparent= "true" > <progressbar android:id= "@+id/load_more_progressbar" Android:layout_width= "36DP" android:layout_height= "36DP" android:padding= "3DP" Andro id:visibility= "visible"/> <textview android:id= "@+id/no_more_textview" Android:layout_ Width= "Wrap_content" android:layout_height= "36DP" android:gravity= "Center" android:text= " Loading "android:textcolor=" #666666 "android:textsize=" 16sp "android:visibility=" visible "/&    Gt </linearlayout></Relativelayout> 


Basic to this pull-up load more on the end. Here is the pull-up pull-up combined with the layout file Main.xml
<relativelayout 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 "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "Com.example.listviewloadmore.MainActivity" > <android.support.v4.widget.swiperefreshlayout androi        D:id= "@+id/swip_index" android:layout_width= "match_parent" android:layout_height= "Match_parent" > <com.example.listviewloadmore.loadmorelistview android:id= "@+id/listview" Android:layout_width= "W Rap_content "android:layout_height=" wrap_content "android:text=" @string/hello_world "> &lt ;/com.example.listviewloadmore.loadmorelistview> </anDroid.support.v4.widget.swiperefreshlayout></relativelayout> 


Mainactivity Code
Package Com.example.listviewloadmore;import Java.util.arraylist;import Android.app.activity;import Android.os.bundle;import Android.os.handler;import Android.support.v4.widget.swiperefreshlayout;import Android.support.v4.widget.swiperefreshlayout.onrefreshlistener;import Android.widget.arrayadapter;import Android.widget.toast;import Com.example.listviewloadmore.loadmorelistview.onloadmore;public class MainActivity Extends Activity implements Onrefreshlistener, Onloadmore {private Loadmorelistview listview;private swiperefreshlayout swip;private int page = 1; arraylist<string> data1 = new arraylist<string> ();p rivate arrayadapter<string> adapter;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Initview (); InitData (1);} private void Initview () {ListView = (Loadmorelistview) Findviewbyid (R.id.listview); Listview.setloadmorelisten (this); Swip = (swiperefreshlayout) Findviewbyid (r.id.swip_index); Swip.setonrefreshlistener (this); Swip.setcolorschemeresources (Android. R.color.holo_blue_light, Android. R.color.holo_red_light, Android. R.color.holo_orange_light,android. R.color.holo_green_light); adapter = new Arrayadapter<string> (this, Android. R.layout.simple_list_item_1, data1);}  /** * Load Data * @param page2 pages */private void initdata (int page2) {//TODO auto-generated method stubif (page2==1) {for (int i = 0; I < 10; i++) {Data1.add ((i+1) + ""); Listview.setadapter (adapter);}} Else{data1.add ("New Plus" + (9+page2) + "one");}} The dropdown Refresh method @overridepublic void Onrefresh () {Toast.maketext (this, "Start Refresh", Toast.length_short). Show (); new Handler (). Postdelayed (New Runnable () {@Overridepublic void run () {if (Swip.isshown ()) {swip.setrefreshing (false);} Toast.maketext (Mainactivity.this, "Refresh completed", Toast.length_short). Show ();}}, 3000);} Load more methods @overridepublic void Loadmore () {page++;new Handler (). postdelayed (New Runnable () {@Overridepublic void run () { InitData (page); Listview.onloadcomplete (); Toast.maketext (MaiNactivity.this, "Load Complete", Toast.length_short). Show (); Adapter.notifydatasetchanged ();}}, 3000);}} 



Up:




SOURCE Address: Click to download

Latest Android ListView drop-down refresh on slide load

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.