Android pull-down refresh and android pull-down

Source: Internet
Author: User

Android pull-down refresh and android pull-down

1. github

Author: https://github.com/chrisbanes/Android-PullToRefresh

My own: https://github.com/zyj1609wz/Android-PullToRefresh

2. Usage

Listview layout File

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.aa.MainActivity"    tools:ignore="MergeRootFrame" >    <com.handmark.pulltorefresh.library.PullToRefreshListView        xmlns:ptr="http://schemas.android.com/apk/res-auto"        android:id="@+id/listview"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:gravity="center"        android:listSelector="#00000000"        ptr:ptrMode="both" /></LinearLayout>


Common Methods

Package com. example. aa; import java. util. arrayList; import java. util. list; import com. handmark. pulltorefresh. library. pullToRefreshBase; import com. handmark. pulltorefresh. library. pullToRefreshBase. mode; import com. handmark. pulltorefresh. library. pullToRefreshBase. onLastItemVisibleListener; import com. handmark. pulltorefresh. library. pullToRefreshBase. onRefreshListener; import com. handmark. pulltorefresh. librar Y. pullToRefreshListView; import android. support. v7.app. actionBarActivity; import android. text. format. dateUtils; import android. widget. listView; import android. widget. toast; import android. OS. asyncTask; import android. OS. bundle; public class MainActivity extends ActionBarActivity {PullToRefreshListView pullToRefreshListView; ListView listView; @ Override protected void onCreate (Bundle savedInstanceState) {Super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); pullToRefreshListView = (PullToRefreshListView) findViewById (R. id. listview); pullToRefreshListView. setOnRefreshListener (new OnRefreshListener <ListView> () {@ Override public void onRefresh (PullToRefreshBase <ListView> refreshView) {String label = DateUtils. formatDateTime (getApplicationContext (), System. currentTimeMi Llis (), DateUtils. FORMAT_SHOW_TIME | DateUtils. FORMAT_SHOW_DATE | DateUtils. FORMAT_ABBREV_ALL); // refreshView of the last refresh time. getLoadingLayoutProxy (). setLastUpdatedLabel ("Last refresh time" + label); // you can specify refreshView for the refresh icon. getLoadingLayoutProxy (). setLoadingDrawable (getResources (). getDrawable (R. drawable. ic_launcher); // after the drop-down is complete, the refreshView is not refreshed. getLoadingLayoutProxy (). setReleaseLabel !! "); // Release the refreshView. getLoadingLayoutProxy (). setRefreshingLabel (""); Toast. makeText (MainActivity. this, "refreshed", Toast. LENGTH_SHORT ). show (); new getdatatask(cmd.exe cute () ;}}); // The listview slides to the last pullToRefreshListView. setOnLastItemVisibleListener (new OnLastItemVisibleListener () {@ Override public void onLastItemVisible () {Toast. makeText (MainActivity. this, "listview to the end", Toast. LENGTH_SHORT ). show () ;}}); pullToRefreshListView. setMode (Mode. PULL_FROM_START); listView = pullToRefreshListView. getRefreshableView (); listView. setAdapter (new Adapter (this, getData ();/*** when the program comes in, refresh the data and automatically refresh */pullToRefreshListView. setRefreshing ();}/*** @ author admin * pullToRefreshListView. onRefreshComplete (); this sentence should be written asynchronously **/private class GetDataTask extends AsyncTask <Void, Void, String >{@ Override protected String doInBackground (Void... params) {try {Thread. sleep (500);} catch (InterruptedException e) {} return "" ;}@ Override protected void onPostExecute (String result) {super. onPostExecute (result); pullToRefreshListView. onRefreshComplete () ;}list <String> getData () {List <String> List = new ArrayList <String> (); for (int I = 0; I <100; I ++) {list. add ("ddd") ;}return list ;}}

 

Refresh Mode

 

// PullToRefreshListView. setMode (Mode. PULL_FROM_START); // pullToRefreshListView is pulled up. setMode (Mode. PULL_FROM_END); // use both the drop-down and pullToRefreshListView. setMode (Mode. BOTH); // do not enable the refresh function pullToRefreshListView. setMode (Mode. DISABLED );

Obtain the current refresh Mode

 

// Obtain the current refresh mode if (pullToRefreshListView. getMode () = Mode. BOTH) {Toast. makeText (MainActivity. this, "the current refresh mode is" + pullToRefreshListView. getMode (), Toast. LENGTH_SHORT ). show ();}

 

Set the refresh sound

 

/*** Add Sound Event Listener */SoundPullEventListener <ListView> soundListener = new SoundPullEventListener <ListView> (this); soundListener. addSoundEvent (State. PULL_TO_REFRESH, R. raw. a1); // start to refresh the display sound soundListener. addSoundEvent (State. RESET, R. raw. a2); // The sound soundListener displayed after the refresh is complete. addSoundEvent (State. REFRESHING, R. raw. a3); // The pullToRefreshListView sound is being refreshed. setOnPullEventListener (soundListener );

 

 

Set pull-down refresh and pull-up to load more listening Methods

 

PullToRefreshListView. setOnRefreshListener (new Refresh ()); /*** listener method ** @ author admin */class Refresh implements OnRefreshListener2 <ListView >{// drop-down @ Override public void onPullDownToRefresh (PullToRefreshBase <ListView> refreshView) {}// pull up @ Override public void onPullUpToRefresh (PullToRefreshBase <ListView> refreshView ){}}

 

Set the font color when refreshing

 

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/container" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "com. example. aa. mainActivity "tools: ignore =" MergeRootFrame "> <com. handmark. pulltorefresh. library. pullToRefreshListView xmlns: ptr = "http : // Schemas.android.com/apk/res-auto "android: id =" @ + id/listview "android: background =" # ffffff "android: layout_width =" fill_parent "android: layout_height =" fill_parent "android: gravity = "center" android: listSelector = "#00000000" ptr: ptrHeaderTextColor = "# FF9900" ptr: ptrHeaderSubTextColor = "#330099" ptr: ptrMode = "both"/> <! -- PtrHeaderTextColor refresh the color of the prompt --> <! -- PtrHeaderSubTextColor refresh the color value of the suboption --> </LinearLayout>

Running result

 

Set the drop-down and pull-up fonts respectively.

 

// Obtain the ILoadingLayout startLayout = pullToRefreshListView displayed in the drop-down list. getLoadingLayoutProxy (true, false); startLayout. setPullLabel ("You can pull, pull... drop-down "); // The prompt startLayout is displayed when you just pull down the page. setRefreshingLabel ("good luck, refreshing... drop-down "); // startLayout when refreshing. setReleaseLabel ("You dare to release, I dare to refresh... drop-down list "); // The prompt displayed when a certain distance is reached // get the ILoadingLayout endLayout = pullToRefreshListView displayed when pulling up. getLoadingLayoutProxy (false, true); endLayout. setPullLabel ("You can pull, pull... pull up "); // The endLayout prompt is displayed when you pull down. setRefreshingLabel ("good luck, refreshing... pull up "); // endLayout when refreshing. setReleaseLabel ("You dare to release, I dare to refresh... "); // The prompt displayed when the distance is reached

 

Common xml configurations

 

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/container" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "com. example. aa. mainActivity "tools: ignore =" MergeRootFrame "> <com. handmark. pulltorefresh. library. pullToRefreshListView xmlns: ptr = "http: // Schemas.android.com/apk/res-auto "android: id =" @ + id/listview "android: layout_width =" fill_parent "android: layout_height =" fill_parent "android: background =" # ffffff "android: gravity = "center" android: listSelector = "#00000000" ptr: ptrHeaderBackground = "@ drawable/background" ptr: ptrHeaderSubTextColor = "#330099" ptr: ptrHeaderTextColor = "# B26B00" ptr: ptrListViewExtrasEnabled = "false" ptr: ptrMode = "both" ptr: pt RRefreshableViewBackground = "@ drawable/b2" ptr: ptrRotateDrawableWhilePulling = "false" ptr: Portable = "true" ptr: ptrShowIndicator = "true"/> <! -- PtrHeaderTextColor refresh the color of the prompt --> <! -- PtrHeaderSubTextColor refresh the color value of the suboption --> <! -- Ptr: Pull the background image on ptrHeaderBackground --> <! -- Small icon displayed in the upper right corner of ptrShowIndicator --> <! -- PtrRefreshableViewBackground background of the entire listview --> <! -- PtrScrollingWhileRefreshingEnabled: whether to allow ListView or GridView scrolling when refreshing. True is better. --> <! -- PtrListViewExtrasEnabled Footer: how to add mPullRefreshListView. true indicates that the headView is added, that is, the headers are refreshed and rolled together when scrolling. --> <! -- PtrRotateDrawableWhilePulling: When the animation is set to rotate, the drop-down determines whether to rotate the animation. --> <! -- Ptr: ptrAnimationStyle; values: flip (flip animation) and rotate (rotation animation ). --> <! -- Ptr: ptrDrawable indicates the setting icon. --> </LinearLayout>

 

Running result

 

3. uncommon things

1. How do I disable log output?

By default, PullToRefresh enables log output. In PullToRefreshBase, You can see static final boolean DEBUG = true;

True: logs are output. False: No logs are output.

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.