SwipeRefreshLayout in Android v4 package official pull-down refresh component
SwipeRefreshLayout is in the v4 package, and corresponding examples are also provided in the corresponding v4Demo. If not, download the latest support-v4
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), restore setEnable (true)
Here is an example of v4:
public class SwipeRefreshLayoutActivity extends Activity implements OnRefreshListener { public static final String[] TITLES = { "Henry IV (1)", "Henry V", "Henry VIII", "Richard II", "Richard III", "Merchant of Venice", "Othello", "King Lear", "Henry IV (1)", "Henry V", "Henry VIII", "Richard II", "Richard III", "Merchant of Venice", "Othello", "King Lear", "Henry IV (1)", "Henry V", "Henry VIII", "Richard II", "Richard III", "Merchant of Venice", "Othello", "King Lear", "Henry IV (1)", "Henry V", "Henry VIII", "Richard II", "Richard III", "Merchant of Venice", "Othello", "King Lear" }; // Try a SUPER quick refresh to make sure we don't get extra refreshes // while the user's finger is still down. private static final boolean SUPER_QUICK_REFRESH = false; private View mContent; private SwipeRefreshLayout mSwipeRefreshWidget; private ListView mList; private Handler mHandler = new Handler(); private final Runnable mRefreshDone = new Runnable() { @Override public void run() { mSwipeRefreshWidget.setRefreshing(false); } }; @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.swipe_refresh_widget_sample); mSwipeRefreshWidget = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget); mSwipeRefreshWidget.setColorScheme(R.color.color1, R.color.color2, R.color.color3, R.color.color4); mList = (ListView) findViewById(R.id.content); ArrayAdapter
arrayAdapter = new ArrayAdapter
(this, android.R.layout.simple_list_item_1, android.R.id.text1, TITLES); mList.setAdapter(arrayAdapter); mSwipeRefreshWidget.setOnRefreshListener(this); mSwipeRefreshWidget.setProgressViewEndTarget(false, 8); } @Override public void onRefresh() { refresh(); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.swipe_refresh_menu, menu); return true; } /** * Click handler for the menu item to force a refresh. */ @Override public boolean onOptionsItemSelected(MenuItem item) { final int id = item.getItemId(); switch(id) { case R.id.force_refresh: mSwipeRefreshWidget.setRefreshing(true); refresh(); return true; } return false; } private void refresh() { mHandler.removeCallbacks(mRefreshDone); mHandler.postDelayed(mRefreshDone, 1000); }}
Swipe_refresh_widget_sample.xml