Simple use of PullToRefreshListView, pullrefreshlistview

Source: Internet
Author: User

Simple use of PullToRefreshListView, pullrefreshlistview

We have introduced the use of XListView before. Here we will introduce a similar list control: PullToRefreshListView, which comes from the open-source project PullToRefresh. There are some other controls in it, which are very convenient to use and directly use the source code:

MainActivity:

Package com. home. testpulltorefresh; import java. util. arrayList; import android. annotation. suppressLint; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. text. format. dateUtils; import android. widget. arrayAdapter; import android. widget. listView; import com. handmark. pulltorefresh. library. ILoadingLayout; import com. handmark. pulltorefresh. library. pullToRefreshBase; I Mport com. handmark. pulltorefresh. library. pullToRefreshBase. mode; import com. handmark. pulltorefresh. library. pullToRefreshBase. onRefreshListener2; import com. handmark. pulltorefresh. library. pullToRefreshListView; import com. home. testpulltorefreshlistview. r; public class MainActivity extends Activity {// list control private PullToRefreshListView mListView; // data set private ArrayList <String> items = new ArrayList <Str Ing> (); private int start = 0; // adapter private ArrayAdapter <String> adapter; private static int refreshCnt = 0; @ SuppressLint ("HandlerLeak ") private Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {switch (msg. what) {case 1: setAdatapter (); mListView. onRefreshComplete (); break; default: break ;}};@overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (sa VedInstanceState); setContentView (R. layout. main); initView (); geneItems ();}/*** set adapter */private void setAdatapter () {if (adapter = null) {adapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, items); mListView. setAdapter (adapter);} else {adapter. notifyDataSetChanged () ;}}/*** initialize the page control */private void initView () {mListView = (PullToRefreshListView) findViewById (R. id. main_lv); // set Set mode mListView. setMode (Mode. BOTH); // supports pulling up/down at the same time // mListView. setMode (Mode. PULL_FROM_END); // only supports Pulling Up // mListView. setMode (Mode. PULL_FROM_START); // only supports Pulling Down. // you can set the Pulling Down listener to Mode. BOTH, listener needs to be set to OnRefreshListener2, and its onPullDownToRefresh and * onPullUpToRefresh Methods * If Mode is set to Mode. PULL_FROM_END or Mode. PULL_FROM_START *, the listener can be set to OnRefreshListener, and its onRefresh Method * listener can also be set to OnRefreshListener 2, * Mode. trigger onPullDownToRefresh when PULL_FROM_START; * Mode. PULL_FROM_END triggers onPullUpToRefresh * // sets the OnRefreshListener2 listener to mListView. setOnRefreshListener (new OnRefreshListener2 <ListView> () {@ Overridepublic void trim (PullToRefreshBase <ListView> refreshView) {setUpdateTime (refreshView); refreshItems ();} @ Overridepublic void onPullUpToRefresh (PullToRefreshBase <ListView> refreshView) {setUpda TeTime (refreshView); geneItems () ;}}); // set the OnRefreshListener listener status // mListView. setOnRefreshListener (new OnRefreshListener <ListView> () {// @ Override // public void onRefresh (PullToRefreshBase <ListView> refreshView) {// setUpdateTime (refreshView ); /// if Mode is set to Mode. PULL_FROM_END: load more business logic here /// if Mode is set to Mode. PULL_FROM_START. Here, refresh the business logic // geneItems (); // refreshItems (); //}); initListViewTipTe Xt ();}/*** prompt text when the initialization list is refreshed */private void initListViewTipText () {// set ILoadingLayout startLabels = mListView. getLoadingLayoutProxy (true, false); startLabels. setPullLabel ("pull-down refresh... "); startLabels. setReleaseLabel ("release now refresh... "); startLabels. setRefreshingLabel ("refreshing... "); // set the text ILoadingLayout endLabels = mListView. getLoadingLayoutProxy (false, true); endLabels. setPullLabel ("pull up to load more... "); endLabels. setR EleaseLabel ("Release loading more... "); endLabels. setRefreshingLabel ("loading... ");}/*** set the Update Time ** @ param refreshView */private void setUpdateTime (PullToRefreshBase <ListView> refreshView) {String label = DateUtils. formatDateTime (getApplicationContext (), System. currentTimeMillis (), DateUtils. FORMAT_SHOW_TIME | DateUtils. FORMAT_SHOW_DATE | DateUtils. FORMAT_ABBREV_ALL); refreshView. getLoadingLayoutProxy (). setLastUpdated Label (label);}/*** request data */private void geneItems () {for (int I = 0; I! = 5; ++ I) {items. add ("refresh cnt" + (++ start);} handler. sendEmptyMessageDelayed (1, 2000);}/*** refresh data */private void refreshItems () {start = ++ refreshCnt; items. clear (); for (int I = 0; I! = 5; ++ I) {items. add ("refresh cnt" + (++ start);} handler. sendEmptyMessageDelayed (1, 2000 );}}
PullToRefreshListView also has some other functions and listeners, which will not be described here.

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.