Android open-source -- PullToRefresh

Source: Internet
Author: User
Tags addall

Android open-source -- PullToRefresh

 

Introduction:PullToRefresh is a framework that supports everything including ListView, GridView, ViewPager, ScrollView, and WebView that can be dragged and refreshed by dragging up, down, left, and right;

[Basic android dependent projects are stored in the library, fragment lists are supported, and ViewPager lists are stored in extras packages]

 

[Dependency project import, as shown in the left figure]

 

1. Support implementation of ListView:

1) XML implementation

 

 
   
  
 

 

2) JAVA code

 

Public class PullListSampleActivity extends Activity {private PullToRefreshListView mPullToRefreshLv; private ArrayAdapter
 
  
MAdapter; private comment list
  
   
MListItems; private String [] mStrings = {Abbaye de Belloc, Abbaye du Mont des Cats, Abertam, cosine, Ackawi, Acorn, Adelost, Affidelice au Chablis, afustm'l Pitu, Airag, airedale, Aisy censid, receivemmentaler, Abbaye de Belloc, Abbaye du Mont des Cats, Abertam, cosine, Ackawi, Acorn, Adelost, Affidelice au Chablis, afustm' l Pitu, Airag, airedale, Aisy censid, Allgauer Emmentaler}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_pull_list_sample); // fill in the Data mPullToRefreshLv = (PullToRefreshListView) findViewById (R. id. my_ptr_lv); mListItems = new shortlist
   
    
(Arrays. asList (mStrings); mAdapter = new ArrayAdapter
    
     
(This, android. R. layout. simple_list_item_1, android. R. id. text1, mListItems); mPullToRefreshLv. setAdapter (mAdapter); // the pull-down refresh sound supports SoundPullEventListener.
     
      
SoundPullEventListener = new SoundPullEventListener
      
        (This); soundPullEventListener. addSoundEvent (State. PULL_TO_REFRESH, R. raw. pull_event); soundPullEventListener. addSoundEvent (State. REFRESHING, R. raw. refreshing_sound); soundPullEventListener. addSoundEvent (State. RESET, R. raw. reset_sound); mPullToRefreshLv. setOnPullEventListener (soundPullEventListener); // listener called during refresh mPullToRefreshLv. setOnRefreshListener (new OnRefreshListener
       
         () {@ Overridepublic void onRefresh (PullToRefreshBase
        
          RefreshView) {String label = DateUtils. formatDateTime (PullListSampleActivity. this, System. currentTimeMillis (), DateUtils. FORMAT_SHOW_TIME | DateUtils. FORMAT_SHOW_DATE | DateUtils. FORMAT_ABBREV_ALL); // sets the LabelmPullToRefreshLv header. getLoadingLayoutProxy (). setLastUpdatedLabel (label); new getlistdatatask(.exe cute () ;}}); mPullToRefreshLv. setOnLastItemVisibleListener (new OnLastItemVisibleListener () {@ Overridepub Lic void onLastItemVisible () {Toast. makeText (PullListSampleActivity. this, all data loaded !, 800). show () ;}});} class GetListDataTask extends AsyncTask
         
           {@ Overrideprotected String [] doInBackground (Void... params) {// mock get datastry {Thread. sleep (3000);} catch (InterruptedException e) {e. printStackTrace ();} return mStrings;} @ Overrideprotected void onPostExecute (String [] result) {mListItems. addAll (Arrays. asList (result); mAdapter. notifyDataSetChanged (); // manually close the header mPullToRefreshLv. onRefreshComplete (); super. onPostExecute (result );}}}
         
        
       
      
     
    
   
  
 

2. Supports the GridView

 

1) XML implementation

Ptr: ptrMode = both indicates that both the up and down/left can be refreshed.

Ptr: ptrDrawable indicates the icon displayed during refresh.

 

     
  
 

2) JAVA code implementation

 

 

Public class PullGridSampleActivity extends Activity {private PullToRefreshGridView mPullToRefreshGridView; private ArrayAdapter
 
  
MAdapter; private comment list
  
   
MListItems; private String [] mStrings = {Abbaye de Belloc, Abbaye du Mont des Cats, Abertam, cosine, Ackawi, Acorn, Adelost, Affidelice au Chablis, afustm'l Pitu, Airag, airedale, Aisy censid, Allgauer Emmentaler}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_pull_grid_sample); mPullToRefreshGridView = (PullToRefreshGridView) findViewById (R. id. my_ptr_gv); mListItems = new sort list
   
    
(); MAdapter = new ArrayAdapter
    
     
(This, android. r. layout. simple_list_item_1, android. r. id. text1, mListItems); mPullToRefreshGridView. setAdapter (mAdapter); // supports the alternative View code when no data is available. Based on your business TextView textView = new TextView (this); LayoutParams params = new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT); params. gravity = Gravity. CENTER; textView. setLayoutParams (params); textView. setText (The data is empty !); MPullToRefreshGridView. setEmptyView (textView); SoundPullEventListener
     
      
PullEventListener = new SoundPullEventListener
      
        (This); pullEventListener. addSoundEvent (State. PULL_TO_REFRESH, R. raw. pull_event); pullEventListener. addSoundEvent (State. RESET, R. raw. reset_sound); pullEventListener. addSoundEvent (State. REFRESHING, R. raw. refreshing_sound); mPullToRefreshGridView. setOnPullEventListener (pullEventListener); mPullToRefreshGridView. setOnRefreshListener (new OnRefreshListener2
       
         () {@ Overridepublic void onPullDownToRefresh (PullToRefreshBase
        
          RefreshView) {String label = DateUtils. formatDateTime (getApplicationContext (), System. currentTimeMillis (), DateUtils. FORMAT_24HOUR); refreshView. getLoadingLayoutProxy (). setLastUpdatedLabel (label); new getdatatask(cmd.exe cute () ;}@ Overridepublic void onPullUpToRefresh (PullToRefreshBase
         
           RefreshView) {String label = DateUtils. formatDateTime (getApplicationContext (), System. currentTimeMillis (), DateUtils. FORMAT_24HOUR); refreshView. getLoadingLayoutProxy (). setLastUpdatedLabel (label); new getdatatask(.exe cute () ;}});} class GetDataTask extends AsyncTask
          
            {@ Overrideprotected String [] doInBackground (Void... params) {try {Thread. sleep (2000);} catch (InterruptedException e) {e. printStackTrace ();} return mStrings;} @ Overrideprotected void onPostExecute (String [] result) {mListItems. addAll (Arrays. asList (result); mAdapter. notifyDataSetChanged (); mPullToRefreshGridView. onRefreshComplete ();}}}
          
         
        
       
      
     
    
   
  
 

3. Implementation of ViewPager

1) XML

 

     
  
 

2) Java Implementation

 

 

Public class PullViewPagerSample extends Activity implements OnRefreshListener
 
  
{Private PullToRefreshViewPager mPullToRefreshViewPager; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_pull_viewpager_sample); mPullToRefreshViewPager = (PullToRefreshViewPager) findViewById (R. id. my_ptr_viewpager); mPullToRefreshViewPager. getRefreshableView (). setAdapter (new MyPagerAdapter (); mPullToRefreshViewPager. setOnRefreshListener (this);} class MyPagerAdapter extends PagerAdapter {private final int [] sDrawables = {R. drawable. wallpaper, R. drawable. wallpaper, R. drawable. wallpaper, R. drawable. wallpaper, R. drawable. wallpaper, R. drawable. wallpaper}; @ Overridepublic int getCount () {return sDrawables. length ;}@ Overridepublic Object instantiateItem (ViewGroup container, int position) {ImageView imageView = new ImageView (container. getContext (); imageView. setImageResource (sDrawables [position]); container. addView (imageView, LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT); return imageView;} @ Overridepublic boolean isViewFromObject (View arg0, Object arg1) {return arg0 = (View) arg1;} @ Overridepublic void destroyItem (ViewGroup container, int position, object object) {container. removeView (View) object); }}@ Overridepublic void onRefresh (PullToRefreshBase
  
   
RefreshView) {try {Thread. sleep (4000); // simulated loading time} catch (InterruptedException e) {e. printStackTrace ();} mPullToRefreshViewPager. onRefreshComplete ();}}
  
 


 


 

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.