android-drop-down refresh

Source: Internet
Author: User

1: Define a ListView control first.

2: Define the template view:

3: Define the view of the bottom view:

4: Write the source code to achieve the effect of the drop-down refresh:

Package Com.huanglinbin.finsh;import Java.util.arraylist;import Java.util.hashmap;import java.util.List;import Java.util.map;import Android.app.activity;import Android.os.bundle;import Android.os.handler;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.abslistview;import Android.widget.abslistview.onscrolllistener;import Android.widget.button;import Android.widget.ListView;import Android.widget.progressbar;import Android.widget.simpleadapter;import Android.widget.toast;public Class Mainactivity extends Activity implements Onscrolllistener {//privatization attribute. Private Simpleadapter sa;private ListView lv;//listviewprivate button but;//buttons private ProgressBar pb;//load bar Private list& Lt Map<string,object>> list;//The view of the bottom view of the collection//listview. Private View moreview;//Async loader. Private Handler handler;//Sets the maximum number of loads that are not loaded when they are exceeded. private int maxnum;//The last visible entry index.    private int lastvisibleindex; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (sAvedinstancestate);                Setcontentview (R.layout.activity_main);        Set the maximum number of data (equivalent to all data in the database) Maxnum = 50;        You first need to find the ListView control.        LV = (ListView) Findviewbyid (R.ID.LISTVIEW1);        The view of the bottom view is instantiated first.                Moreview = Getlayoutinflater (). Inflate (R.layout.but,null);        Initializes the data.        List = new arraylist<map<string,object>> ();        Simulate the database.        for (int i=0;i<20;i++) {map<string,object> Map = new hashmap<string, object> ();        Map.put ("TV1", "section" +i+ "title");        Map.put ("TV2", "section" +i+ "Contents");        List.add (map);        }//Instantiate emulator. SA = new Simpleadapter (mainactivity.this, List, R.layout.item, new string[]{"TV1", "TV2"}, New        INT[]{R.ID.TV1,R.ID.TV2}); Loads the view at the bottom of the view.        Note Be sure to setadapter before loading the emulator.        Lv.addfooterview (Moreview);                Lv.setadapter (SA);        A listener event that binds the ListView to a scroll.         Lv.setonscrolllistener (Mainactivity.this);      Gets the button.        but = (Button) Findviewbyid (r.id.but);         PB = (ProgressBar) Findviewbyid (R.ID.PB);        The asynchronous loader.              Handler = new Handler ();       A listener event that binds the button to a fixed point. /* But.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//shows the progress bar. Pb.setvisibility (view.visible);//Displays the button as not visible. But.setvisibility (View.gone);//The loading of asynchronous data here. (Parameter 1:new run control, Parameter 2: Time of Asynchronous load) handler.postdelayed (new Runnable () {@Overridepublic void Run () {//Load more data. Lostmove ();//The progress bar is displayed. Pb.setvisibility (view.visible);//Displays the button as not visible. But.setvisibility (View.gone);//Notifies the ListView to refresh the data. Sa.notifydatasetchanged ();}},2000);});    */}//define a method to load data each time.    private void Lostmove () {//Gets the data in the adapter.    int count = Sa.getcount ();    Judgment when the data is not being loaded.    if (count+5<maxnum) {//Load data 5 each time.    for (int i=count;i<count+5;i++) {map<string,object> map1 = new hashmap<string, object> ();    Map1.put ("TV1", "section" +i+ "title");    Map1.put ("TV2", "section" +i+ "Contents");    List.add (MAP1); }   }else{//Data loading is less than 5 data load.    for (int j=count;j<maxnum;j++) {map<string,object> map1 = new hashmap<string, object> ();    Map1.put ("TV1", "section" +j+ "title");    Map1.put ("TV2", "section" +j+ "Contents");    List.add (MAP1); }}} public void Onscroll (Abslistview view, int firstvisibleitem,int visibleitemcount, int totalitemcount) {//Scroll The callback is stopped until the scroll is stopped. Callback once when clicked. Firstvisibleitem: The first list item ID currently visible (starting at 0)//visibleitemcount: The number of list items currently visible (small half is counted)//totalitemcount: List items total number// Calculates the index of the last visible entry Lastvisibleindex = firstvisibleitem+visibleitemcount-1;//The view of the ListView at the bottom is removed when all the list items are equal to the maximum number of data. if (Totalitemcount = = maxnum+1) {//Remove bottom ListView. Lv.removefooterview (Moreview); Toast.maketext (Mainactivity.this, "The data is all loaded! ", Toast.length_short). Show ();}} public void onscrollstatechanged (Abslistview view, int scrollstate) {//slip to the bottom of the load, Determine if the ListView has stopped scrolling and the last visible entry equals the adapter entry if (scrollstate==onscrolllistener.scroll_state_idle&& Lastvisibleindex==sa.getcount ()) {//The progress bar is displayed. Pb.setvisibility (view.visible); handLer.postdelayed (New Runnable () {@Overridepublic void Run () {//Load more data. Lostmove ();//The progress bar is displayed. Pb.setvisibility (view.visible);//Notifies the ListView to refresh the data.       Sa.notifydatasetchanged ();}},2000);}} }

5::

android-drop-down refresh

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.