Pulltorefresh (1) usage Introduction: pull more from pull-down refresh, and pull from pulltorefresh

Source: Internet
Author: User

Pulltorefresh (1) usage Introduction: pull more from pull-down refresh, and pull from pulltorefresh

In Android development, pull and refresh are required for pull-down loading in many projects. How can this function be implemented, you can use the android-PullToRefresh library written by a third-party plug-in chrisbanes, or the SwipeRefreshLayout officially launched by Google.
I personally think PullToRefresh is easy to use and scalable. It is used in many external enterprise projects. So I will introduce the usage of this plug-in.

PullToRefresh support coefficient:

    • ListView
    • ExpandableListView
    • GridView
    • WebView
    • ScrollView
    • HorizontalScrollView
    • ViewPager

Download library path: https://github.com/chrisbanes/Android-PullToRefresh

Download completed. Import the project. Add the newly introduced project to your project library.

Go straight to the topic for the introduction!

Ps: it supports a lot of controls, but it can be used by others. We use a lot of listview to pull down and refresh as an instance.

I. Write Layout

1 <com.handmark.pulltorefresh.library.PullToRefreshListView2         android:id="@+id/plistview"3         android:layout_width="match_parent"4         android:layout_height="match_parent"5         />

Ii. binding controls

1 private PullToRefreshListView pListView; // PullToRefreshListView control object 2 @ Override3 protected void onCreate (Bundle savedInstanceState) {4 super. onCreate (savedInstanceState); 5 setContentView (R. layout. listview_layout); 6 pListView = (PullToRefreshListView) findViewById (R. id. plistview); 7}

3. bind an adapter to listview

1 ArrayList <String> arrayList = new ArrayList <String> (); 2 // initialize the adapter 3 adapter = new ArrayAdapter <String> (this, R. layout. item_layout, R. id. TV _item_name, arrayList); 4 adapter. add ("snail Il"); 5 adapter. add ("_ snail Il"); 6 adapter. add ("_ snail Il"); 7 adapter. add ("___ snail Il"); 8 // bind the adapter 9 pListView. setAdapter (adapter );

 

Iv. Set the refresh Mode

1/* 2 * Set PullToRefresh refresh Mode 3 * BOTH: BOTH pull-up and pull-down refresh support 4 * DISABLED: Disable pull-up and pull-down refresh 5 * PULL_FROM_START: only pull-down refresh (default) 6 * PULL_FROM_END: only pull-up refresh 7 * MANUAL_REFRESH_ONLY: only manual triggering of 8 **/9 pListView is allowed. setMode (Mode. PULL_FROM_START );

5. Bind a refresh listener event

// Set the refresh listener pListView. setOnRefreshListener (new OnRefreshListener <ListView> () {@ Override public void onRefresh (PullToRefreshBase <ListView> refreshView) {String str = DateUtils. formatDateTime (MainActivity. this, System. currentTimeMillis (), DateUtils. FORMAT_SHOW_TIME | DateUtils. FORMAT_SHOW_DATE | DateUtils. FORMAT_ABBREV_ALL); // sets the refresh label pListView. getLoadingLayoutProxy (). setRefreshingLabel ("refreshing"); // set the drop-down label pListView. getLoadingLayoutProxy (). setPullLabel ("pull-down refresh"); // sets the release label pListView. getLoadingLayoutProxy (). setReleaseLabel ("Release start refresh"); // set the last refresh prompt label refreshView. getLoadingLayoutProxy (). setLastUpdatedLabel ("Last Update time:" + str); // new mytask(cmd.exe cute ();}});

Load the data code (my local data is usually obtained from the network ):

1 private class MyTask extends AsyncTask <Void, Void, ArrayList <String> {2 3 @ Override 4 protected ArrayList <String> doInBackground (Void... params) {5 try {6 Thread. sleep (2000); // sleep for 2 seconds, delayed loading of data 7} catch (InterruptedException e) {8 e. printStackTrace (); 9} 10 ArrayList <String> mArrayList = new ArrayList <String> (); 11 for (int I = 0; I <5; I ++) {12 counter ++; 13 mArrayList. add ("-----" + String. valueOf (counter) + "-------"); 14} 15 return mArrayList; 16} 17 18 @ Override19 protected void onPostExecute (ArrayList <String> result) {20 for (String string: result) {21 adapter. add (string); 22} 23 pListView. onRefreshComplete (); // after the data is loaded to the adapter, the refresh is complete, 24 super. onPostExecute (result); 25} 26 27}

Ps:PListView. onRefreshComplete ();This method must be called after data loading is complete. Otherwise, the header refresh on the interface will remain.

You can pull down and refresh the five simple ones. As follows:

However, we still need to pull and load more resources. How can we do this? We only need to modify part 4 and Part 5 to support the pull and load more.

First of all, let's talk about our fourth model being BOTH.

Then, add a method to identify whether to pull up or pull down the base class file PullToRefreshBase of the puutorefresh library. The Code is as follows:

1 // determine whether the header is displayed. If it is displayed, the header is displayed in a drop-down mode. True is the drop-down 2 public boolean isShownHeader () {3 return getHeaderLayout (). isShown (); 4} 5 // determine whether the lower part is displayed. If the lower part is displayed, the lower part is displayed. True: Pull up 6 public boolean isShownFooter () {7 return getFooterLayout (). isShown (); 8}

Last modified our listening events

1 // set refresh listener 2 pListView. setOnRefreshListener (new OnRefreshListener <ListView> () {3 @ Override 4 public void onRefresh (PullToRefreshBase <ListView> refreshView) {5 6 String str = DateUtils. formatDateTime (MainActivity. this, System. currentTimeMillis (), DateUtils. FORMAT_SHOW_TIME | DateUtils. FORMAT_SHOW_DATE | DateUtils. FORMAT_ABBREV_ALL); 7 // pull-down refresh business code 8 if (refreshView. isShownHeader () {9 pListView. getLoadingLayoutProxy (). setRefreshingLabel ("refreshing"); 10 pListView. getLoadingLayoutProxy (). setPullLabel ("pull-down refresh"); 11 pListView. getLoadingLayoutProxy (). setReleaseLabel ("Release start refresh"); 12 refreshView. getLoadingLayoutProxy (). setLastUpdatedLabel ("Last Update time:" + str); 13 new mytask(cmd.exe cute (); 14} 15 // load more business code 16 if (refreshView. isShownFooter () {17 pListView. getLoadingLayoutProxy (). setRefreshingLabel ("loading"); 18 pListView. getLoadingLayoutProxy (). setPullLabel ("pull up to load more"); 19 pListView. getLoadingLayoutProxy (). setReleaseLabel ("Release starts loading"); 20 refreshView. getLoadingLayoutProxy (). setLastUpdatedLabel ("last loading time:" + str); 21 new mytask(cmd.exe cute (); 22} 23} 24}); 25

In this way, we support both the up-down and down-pull operations. As follows:

Additional code: Link: http://pan.baidu.com/s/1qWOBTla password: 12w5

 

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.