[Basic usage of PullToRefresh series] Pull and refresh controls on Android
Reprinted Please note: http://blog.csdn.net/duguang77/article/details/40921601
Author information:
Chris Banes: Https://github.com/chrisbanes
PullToRefresh control: https://github.com/chrisbanes/Android-PullToRefresh
Baidu Network Disk: http://pan.baidu.com/s/1o6umifw
(1) import method:
1. After the download, import extras, library, and sample to Eclipse.
2. An error may be reported during import. Change the dependency package location.
(2) method of use: We generally use a relatively large number of simple ListView. Of course, in other cases such as the GridView and WebView, the author also has a directly written control, which can be used directly.
The following describes the basic methods used.
1. XML layout File Code
<RelativeLayout 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" tools:context="com.example.testpulltoreresh.MainActivity" > <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pl_refresh" android:layout_width="match_parent" android:layout_height="match_parent" /></RelativeLayout>
2. Set the PullToRefresh listening event
MPullList. setOnRefreshListener (new OnRefreshListener2 () {@ Overridepublic void onPullDownToRefresh (PullToRefreshBase refreshView) {// pull-down refresh event triggered // get formatted time String label = DateUtils. formatDateTime (getApplicationContext (), System. currentTimeMillis (), DateUtils. FORMAT_SHOW_TIME | DateUtils. FORMAT_SHOW_DATE | DateUtils. FORMAT_ABBREV_ALL); // update LastUpdatedLabelrefreshView. getLoadingLayoutProxy (). setLastUpdatedLabel (label); // enable the thread simulation interface to fill in data new getdatatask(.exe cute () ;}@ Overridepublic void onPullUpToRefresh (PullToRefreshBase refreshView) {// upload the event triggered by loading // enable the thread simulation interface to fill in the Data new getdatatask(cmd.exe cute ();}});
3. Set the drop-down list to check whether the above function is available.
// Set the Mode of the PullToRefreshListView. // Mode. DISABLED: Disable all ListView actions and refresh processes // Mode. MANUAL_REFRESH_ONLY indicates the discarded action. The drop-down action processing is disabled, but the refresh status can be set manually. // Mode. PULL_DOWN_TO_REFRESH is discarded, only the drop-down action // Mode. PULL_UP_TO_REFRESH indicates the deprecated action. Only the upstreaming action is allowed. // Mode. PULL_FROM_END only supports pull-up actions // Mode. PULL_FROM_START only allows you to refresh the view from the beginning. Start with the top or exit, depending on the scroll direction. Only the drop-down action // Mode. BOTH is used to pull up all the actions mPullList. setMode (Mode. BOTH );
Or directly set the MODE attribute through the XML file.
ptr:ptrMode="both"
Note that you must add a namespace.
xmlns:ptr="http://schemas.android.com/apk/res-auto"
<RelativeLayout 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" tools:context="com.example.testpulltoreresh.MainActivity" > <com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/pl_refresh" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#00000000" android:divider="#19000000" android:dividerHeight="0.5dp" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true" ptr:ptrMode="both"/></RelativeLayout>
4. Set the drop-down list to display the loading prompt.
// Set the prompt mPullList for loading when PullRefreshListView is submitted. setMode (Mode. BOTH); mPullList. getLoadingLayoutProxy (false, true ). setPullLabel ("pull up and load... "); mPullList. getLoadingLayoutProxy (false, true ). setRefreshingLabel ("loading... "); mPullList. getLoadingLayoutProxy (false, true ). setReleaseLabel ("release to load more... "); // set mPullList when loading the PullRefreshListView drop-down. getLoadingLayoutProxy (true, false ). setPullLabel ("pull-down refresh... "); mPullList. getLoadingLayoutProxy (true, false ). setRefreshingLabel ("loading... "); mPullList. getLoadingLayoutProxy (true, false ). setReleaseLabel ("release to load more... ");
5. Set the drop-down list to display the refreshed animation.
Ptr: ptrAnimationStyle = "flip" flip Animation
<Pre name = "code" class = "java"> ptr: ptrAnimationStyle = "rotate" <span style = "white-space: pre"> </span> Rotation Animation
<com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto" ....................... ptr:ptrAnimationStyle="flip"/>
Rotation Animation:
Flip Animation
6. Change the icon in the drop-down list
<com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto" ........ ptr:ptrDrawable="@drawable/ic_rockets" />
7. Other common attributes
// Set the background color ptrRefreshableViewBackground of the entire mPullRefreshListView // set the drop-down Header or the background color ptrHeaderBackground of the Footer. // set the color ptrHeaderTextColor of the Header and the text in the Footer. // color of the last refresh time ptrHeaderSubTextColor // if it is true, the icon will appear in mPullRefreshListView, the upper right and lower right corner are interesting. PtrShowIndicator // The following two are respectively set to pull Header or pull up the font type color in Footer and so on. PtrHeaderTextAppearance, ptrSubHeaderTextAppearance // when the animation is set to rotate, the drop-down determines whether to rotate. PtrRotateDrawableWhilePulling // whether to allow ListView or GridView scrolling when refreshing. True is better. PtrScrollingWhileRefreshingEnabled // determines the Header, in which way the Footer is added to the mPullRefreshListView, and true is added to the headView, that is, the headers will be rolled together when they are refreshed. PtrListViewExtrasEnabled // the last two are important for user experience. Select other attributes. // Note: many of the above attributes can be controlled by code. If necessary, you can directly view the mPullRefreshListView. set attribute name. // all the attributes supported by PullToRefresh are listed above ~~
All codes of my Demo project
Public class MainActivity extends Activity {private PullToRefreshListView mPullList; private parameter list <String> mListItems; private ArrayAdapter <String> mAdapter; private String [] mStrings = {"I am very kind ", "I am gentle", "I am a naughty girl", "I am alilang", "I am a big gray wolf", "I am a goat"}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initView (); setEventListener (); initData ();}/*** initialization control */private void initView () {mPullList = (PullToRefreshListView) findViewById (R. id. pl_refresh);}/*** set listener */private void setEventListener () {mPullList. setOnRefreshListener (new OnRefreshListener2 () {@ Overridepublic void onPullDownToRefresh (PullToRefreshBase refreshView) {// pull-down refresh event triggered // get formatted time String label = DateUtils. formatDateTime (getApplicationContext (), System. currentTimeMillis (), DateUtils. FORMAT_SHOW_TIME | DateUtils. FORMAT_SHOW_DATE | DateUtils. FORMAT_ABBREV_ALL); // update LastUpdatedLabelrefreshView. getLoadingLayoutProxy (). setLastUpdatedLabel (label); // enable the thread simulation interface to fill in data new getdatatask(.exe cute () ;}@ Overridepublic void onPullUpToRefresh (PullToRefreshBase refreshView) {// upload the event triggered by loading // enable the thread simulation interface to fill in the Data new getdatatask(cmd.exe cute ();}});} /*** initialize data */private void initData () {// set the PullToRefreshListView mode mPullList. setMode (Mode. BOTH); // set the PullRefreshListView loading prompt mPullList. setMode (Mode. BOTH); mPullList. getLoadingLayoutProxy (false, true ). setPullLabel ("pull up and load... "); mPullList. getLoadingLayoutProxy (false, true ). setRefreshingLabel ("loading... "); mPullList. getLoadingLayoutProxy (false, true ). setReleaseLabel ("release to load more... "); // set mPullList when loading the PullRefreshListView drop-down. getLoadingLayoutProxy (true, false ). setPullLabel ("pull-down refresh... "); mPullList. getLoadingLayoutProxy (true, false ). setRefreshingLabel ("loading... "); mPullList. getLoadingLayoutProxy (true, false ). setReleaseLabel ("release to load more... "); mListItems = new listlist <String> (); mListItems. addAll (Arrays. asList (mStrings); mAdapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, mListItems); mPullList. setAdapter (mAdapter);} private class GetDataTask extends AsyncTask <Void, Void, String [] >{@ Overrideprotected String [] doInBackground (Void... params) {try {Thread. sleep (2000);} catch (InterruptedException e) {} return null;} @ Overrideprotected void onPostExecute (String [] result) {if (mPullList. isHeaderShown () {mListItems. addFirst ("I Am a newly incorporated -- pull-down refresh");} else if (mPullList. isFooterShown () {mListItems. addLast ("I am the one who makes you feel back-Attach");} mAdapter. notifyDataSetChanged (); // call refresh to complete mPullList. onRefreshComplete (); super. onPostExecute (result );}}}
Demo: http://pan.baidu.com/s/1c03tVVE
Reprinted Please note: http://blog.csdn.net/duguang77/article/details/40921601