Pull-up Refresh drop-down load control-pulltorefresh

Source: Internet
Author: User

    • In many software, we will use the pull-up refresh, and most will also have a pull-load function, Pulltorefresh This control can help us achieve this effect.

    • To use this space first we need to guide the package

Layout file
    • A simple Pulltorefreshlistview is added to the layout file.
<?xml version= "1.0" encoding= "Utf-8"?><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 " android:orientation  =" vertical " tools:context  = " Com.lingzhuo.testfrash.MainActivity ";     <com.handmark.pulltorefresh.library.PullToRefreshListViewandroid:id="@+id/ Pulltorefreshlistview "android:layout_width=" Match_parent "android:layout_height ="Match_parent">                        </com.handmark.pulltorefresh.library.PullToRefreshListView></linearlayout>
Logic code
 Public  class mainactivity extends appcompatactivity {    PrivatePulltorefreshlistview Pulltorefreshlistview;PrivateList<string> dataList;protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Init (); Myadapter adapter =NewMyadapter (DataList, Getapplicationcontext ()); Pulltorefreshlistview.setadapter (adapter);/** * * Set Pulltorefresh Refresh mode * BOTH: Both pull-up refresh and drop-down refresh support * DISABLED: Disable pull-down refresh * Pull_from_start: Only support pull-down refresh (default) * Pull_from_end: Support only pull-up refresh * manual_refresh_only: only allow manual trigger */Pulltorefreshlistview.setmode (PullToRefreshBase.Mode.BOTH); Pulltorefreshlistview.setonscrolllistener (NewAbslistview.onscrolllistener () {@Override             Public void onscrollstatechanged(Abslistview view,intscrollstate) {}//Here I can distinguish between the current pull-up or drop-down event by judging the first visible item in the sliding event, while setting the text content of the refreshed hint, so that the setting has a certain flaw, I will explain in detail below, as to why the following listener has implemented the pull-up and drop-down distinctions, You'll find the problem if you don't add that judgment yourself a few times.             Public void onscroll(Abslistview view,intFirstvisibleitem,intVisibleItemCount,intTotalitemcount) {if(firstvisibleitem==0){//Set Refresh tabPulltorefreshlistview.getloadinglayoutproxy (). Setrefreshinglabel ("Refreshing");//Set drop-down tabPulltorefreshlistview.getloadinglayoutproxy (). Setpulllabel ("Drop-down refresh");//Set release labelPulltorefreshlistview.getloadinglayoutproxy (). Setreleaselabel ("Release immediate Refresh"); }Else{//Set Refresh tabPulltorefreshlistview.getloadinglayoutproxy (). Setrefreshinglabel ("Loading ...");//Set drop-down tabPulltorefreshlistview.getloadinglayoutproxy (). Setpulllabel ("Load More ...");//Set release labelPulltorefreshlistview.getloadinglayoutproxy (). Setreleaselabel ("Load More ..."); }            }        });//Set Refresh ListenerPulltorefreshlistview.setonrefreshlistener (NewPulltorefreshbase.onrefreshlistener2<listview> () {@Override             Public void Onpulldowntorefresh(pulltorefreshbase<listview> Refreshview) {//Set prompt label for previous refreshRefreshview.getloadinglayoutproxy (). Setlastupdatedlabel ("Last Update time:"+NewSimpleDateFormat ("Hh:mm:ss"). Format (System.currenttimemillis ())); for(inti =0; I <Ten; i++) {Datalist.add ("Drop-down loading"+ i); } pulltorefreshlistview.postdelayed (NewRunnable () {@Override                     Public void Run() {pulltorefreshlistview.onrefreshcomplete (); }                }, +); }@Override             Public void Onpulluptorefresh(pulltorefreshbase<listview> Refreshview) {//Set prompt label for previous refreshRefreshview.getloadinglayoutproxy (). Setlastupdatedlabel ("Last Update time:"+NewSimpleDateFormat ("Hh:mm:ss"). Format (System.currenttimemillis ())); Pulltorefreshlistview.postdelayed (NewRunnable () {@Override                     Public void Run() {pulltorefreshlistview.onrefreshcomplete (); }                }, +);    }        }); }Private void Init() {Pulltorefreshlistview = (Pulltorefreshlistview) Findviewbyid (R.id.pulltorefreshlistview); DataList =NewArraylist<> (); for(inti =0; I <Ten; i++) {Datalist.add ("This is a new piece of data."+ i); }    }}
    • After the data is updated, this code must be called Pulltorefreshlistview.onrefreshcomplete ();
    • But that's how I realized it.
                pullToRefreshListView.postDelayed(new Runnable() {                    @Override                    publicvoidrun() {                        pullToRefreshListView.onRefreshComplete();                    }                },1000);
    • Because Pulltorefreshlistview implements pull-down or pull-up loading, it is possible to call the Onrefreshcomplete method to stop the flush operation when the pull-up is complete, but it may not be valid, the test causes, the time is too short to refresh the fetch data, The problem will occur

    • We can delay about 1 seconds, and in calling the Onrefreshcomplete method, you can fix the problem

        mListView.postDelayed(new Runnable() {            @Override            publicvoidrun() {                mListView.onRefreshComplete();            }        1000);
    • Remember to do it behind the setadpter, otherwise it's invalid.
    • In fact, we do not have to pay too much attention, think I just write demo, the real use of this framework, we generally from the Internet to obtain data to load the display, generally will have a certain delay.

    • My judgment on pull-up or pull-down is based on whether the first item in the list is currently displayed, which results in when our first page is not filled, that is, when the first item in the list is displayed, while the first entry in our current list is the first item, the drop-down display of the hint text, Or the same text as the pull-up display.

    • Here I saw someone on the internet to modify the source code to achieve the distinction between pull or pull, may be I too food, do not know how to import third-party library, tried a half-day did not succeed, Khan!!! If you are interested, you can try this blogger's method of changing the source code, if successful, remember the message taught me, thank you.

Pull-up Refresh drop-down load control-pulltorefresh

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.