Android implementation pull up load more and pull down refresh function (ListView) _android

Source: Internet
Author: User
Tags addall

First for you to introduce Andorid5.0 original Drop-down refresh simple implementation.

First on the effect chart;

It looks a lot better than the bars in the previous version 19.1.0. It's easy to use.

 <framelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:id=" @+id/container "android:layout_width=" Match_parent "android:layout_height=" "Match_parent" tools:ignore= "Mergerootframe" > <android.support.v4.widget.swiperefreshlayout android:id= "@+" Id/swipe_container "android:layout_width=" match_parent "android:layout_height=" match_parent "> <ListView andr
 Oid:id= "@+id/list" android:layout_width= "match_parent" android:layout_height= "match_parent" > </ListView>

</android.support.v4.widget.SwipeRefreshLayout> </FrameLayout> Package hi.xiaoyu.swiperefreshlayout;

Import Hi.xiaoyu.swiperefreshlayout.adapter.TestAdapter;
Import java.util.ArrayList;

Import java.util.List;
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.ListView;
 public class Mainactivity extends activity implements Onrefreshlistener {private swiperefreshlayout swipelayout;
 Private ListView ListView;
 Private list<string> Listdatas;

 Private Testadapter adapter;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.activity_main);
 Swipelayout = (swiperefreshlayout) Findviewbyid (R.id.swipe_container);
 ListView = (ListView) Findviewbyid (r.id.list);
 Swipelayout.setonrefreshlistener (this); Swipelayout.setcolorschemeresources (Android. R.color.holo_orange_dark, Android. R.color.holo_green_light, Android. R.color.holo_orange_light, Android.
 R.color.holo_red_light);
 Listdatas = new arraylist<string> ();
 for (int i = 0; i < i++) {Listdatas.add ("item" + i);
 } adapter = new Testadapter (this, Listdatas, R.layout.test_item);
 Listview.setadapter (adapter); public void Onrefresh () {new Handler (). postdelayed (nEW Runnable () {public void run () {swipelayout.setrefreshing (false);
  Listdatas.addall (Listdatas);
  Adapter.notifydatasetchanged ();
 }, 3000);
 }

}

A few lines of code can be achieved Drop-down refresh, the effect is also good, do not introduce a third-party jar, the only drawback is that there is no pull load, do not know what the Google engineers based on the considerations, I hope to see in the next version. But their own changes under the source code plus a pull is relatively simple, combined with the previous version of the refresh effect of the pull effect is not bad.

Second, Android implementation of the pull load more features and Drop-down refresh function
Using the current comparison of Fire Pulltorefresh, he is currently implementing a better Drop-down refresh of the class library.
At present he supports the control to have: ListView, Expandablelistview,gridview,webview and so on.
First of course the first step is to import Libriay to our project, the specific import method, here no longer repeat.
Here's an example of ListView, and of course the rest is similar to this one.
1, layout file Activity_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 "
 tools:context= ". Mainactivity ">
 
 <com.handmark.pulltorefresh.library.pulltorefreshlistview
 android:id=" @+id/pull_ Refresh_list "
 android:layout_width=" fill_parent "
 android:layout_height=" fill_parent "/>
</ Relativelayout>

2, to achieve the function of Drop-down refresh is very simple, only to achieve Onrefreshlistener Onrefresh method can be.

Here's how to implement the pull and drop down to perform different operations separately.
The principle is: to distinguish pull or drop down, depending on the visible state class of the layout shown by Drop-down and pull, and then perform the appropriate action.
Under the Com.handmark.pulltorefresh.library package of the Pulltorefresh class library, open Pulltorefreshbase.java and add the following code at the end of the class:

public Boolean Isheadershown () {return

 getheaderlayout (). Isshown ();

}

public Boolean Isfootershown () {return

 getfooterlayout (). Isshown ();
}

3. The code for the activity is as follows:

public class Mainactivity extends Listactivity {private Pulltorefreshlistview mpulltorefreshlistview;
 Private linkedlist<string> mitemlist;
 Private arrayadapter<string> adapter;
 private context;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.activity_main);
 context = this;
 InitData (); adapter = new Arrayadapter<string> (this, Android.
 R.layout.simple_list_item_1, mitemlist);
 Initializes the control Mpulltorefreshlistview = (Pulltorefreshlistview) Findviewbyid (r.id.pull_refresh_list);
 ListView Mlistview = Mpulltorefreshlistview.getrefreshableview (); 
 Mlistview.setadapter (adapter);
 Set Pull-to-refresh mode to Mode.both Mpulltorefreshlistview.setmode (Mode.both); Sets the pull down event Mpulltorefreshlistview.setonrefreshlistener (new Onrefreshlistener<listview> () {@Override public V OID Onrefresh (pulltorefreshbase<listview> refreshview) {if (Refreshview.isheadershown ()) {TOAST.MAketext (Context, "Drop-down refresh", Toast.length_short). Show ();
   Dropdown refreshes the business code}else {toast.maketext (context, "pull load more", Toast.length_short). Show ();
  
 Pull load more business code}}});
 private void InitData () {//initialization data Mitemlist = new linkedlist<string> ();
  
 Mitemlist.addall (arrays.aslist (data)); Private string[] data = new string[]{"Data1", "Data2", "Data3", "data4", "Data5", "Data6", "Data1", "Data2", "Data3", "data
4 "," Data5 "," data6 "};
 }

As shown in the code above, in the Onrefresh implementation code to distinguish between pull or pull, the key code is as follows:

if (Refreshview.isheadershown ()) {
   Toast.maketext (context, Drop-down Refresh, Toast.length_short). Show ();
   Dropdown refreshes the business code
  }else {
   toast.maketext (context, "pull load more", Toast.length_short). Show ();
   Pull load more business code
  }

Now that you are running the project, you can get the demo results.

This article has been sorted into the "Android Drop-down refresh loading effect," Welcome to study.

The article is rich in content, I hope to learn more about Android implementation of the pull load and the Drop-down refresh function helpful.

Related Article

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.