Android SwipeRefreshLayout official pull-down refresh control introduction, android pull-down refresh
Google provides an official pull-down refresh control, SwipeRefreshLayout. I personally feel pretty good! Thanks to the traditional pull-down refresh, this gives a fresh feeling (Gmail mailbox has already used this pull-down refresh ).
SwipeRefreshLayout is in the V4 package, and corresponding examples are also provided in the corresponding V4 Demo.
SwipeRefreshLayout can have only one direct sub-View, which may be a ListView, a Layout, or other components to be refreshed.
SetOnRefreshListener is used to listen for refreshing actions. The SwipeRefreshLayout drop-down will have a refreshing effect and trigger this listener.
If you need a refresh animation, setRefreshing (true), stop: setRefreshing (false)
If you want to disable refresh animation and gesture response, ssetEnable (false) and restore: setEnable (true)
Google also provides V4 compatibility packages on its official website:
Take a look at the API:
Similar to XlistView, It is very convenient to use. Here is a simple example of the four commonly used methods.
1. layout file:
[Html]View plaincopy
- <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
- Android: layout_width = "match_parent"
- Android: layout_height = "match_parent">
- <Android. support. v4.widget. SwipeRefreshLayout
- Android: id = "@ + id/id_swipe_ly"
- Android: layout_width = "match_parent"
- Android: layout_height = "match_parent">
- <ListView
- Android: id = "@ + id/id_listview"
- Android: layout_width = "match_parent"
- Android: layout_height = "match_parent">
- </ListView>
- </Android. support. v4.widget. SwipeRefreshLayout>
- </RelativeLayout>
2. MainActivty:
[Java]View plaincopy
- Package com. zhy. swiperefreshlayoutdemo;
- Import java. util. ArrayList;
- Import java. util. Arrays;
- Import java. util. List;
- Import android. annotation. SuppressLint;
- Import android. app. Activity;
- Import android. OS. Bundle;
- Import android. OS. Handler;
- Import android. support. v4.widget. SwipeRefreshLayout;
- Import android. util. Log;
- Import android. widget. ArrayAdapter;
- Import android. widget. ListView;
- Public class MainActivity extends Activity implements SwipeRefreshLayout. OnRefreshListener
- {
- Private static final int REFRESH_COMPLETE = 0X110;
- Private SwipeRefreshLayout mSwipeLayout;
- Private ListView mListView;
- Private ArrayAdapter <String> mAdapter;
- Private List <String> mDatas = new ArrayList <String> (Arrays. asList ("Java", "Javascript", "C ++", "Ruby", "Json ",
- "HTML "));
- Private Handler mHandler = new Handler ()
- {
- Public void handleMessage (android. OS. Message msg)
- {
- Switch (msg. what)
- {
- Case REFRESH_COMPLETE:
- MDatas. addAll (Arrays. asList ("Lucene", "Canvas", "Bitmap "));
- MAdapter. notifyDataSetChanged ();
- MSwipeLayout. setRefreshing (false );
- Break;
- }
- };
- };
- @ SuppressLint ("InlinedApi ")
- Protected void onCreate (Bundle savedInstanceState)
- {
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_main );
- MListView = (ListView) findViewById (R. id. id_listview );
- MSwipeLayout = (SwipeRefreshLayout) findViewById (R. id. id_swipe_ly );
- MSwipeLayout. setOnRefreshListener (this );
- MSwipeLayout. setColorScheme (android. R. color. holo_blue_bright, android. R. color. holo_green_light,
- Android. R. color. holo_orange_light, android. R. color. holo_red_light );
- MAdapter = new ArrayAdapter <String> (this, android. R. layout. simple_list_item_1, mDatas );
- MListView. setAdapter (mAdapter );
- }
- Public void onRefresh ()
- {
- MHandler. sendEmptyMessageDelayed (REFRESH_COMPLETE, 2000 );
- }
- }
:
Reference: http://blog.csdn.net/lmj623565791/article/details/24521483