Android v4包中的 SwipeRefreshLayout 官方的下拉刷新组件

得意犹堪夸世俗,诏黄新湿字如鸦。这篇文章主要讲述Android v4包中的 SwipeRefreshLayout 官方的下拉刷新组件相关的知识,希望能为你提供帮助。
SwipeRefreshLayout在v4包下,相应的v4Demo中也有相应的样例。假设没有请下载最新support-v4
SwipeRefreshLayout 仅仅能有一个直接子View,可能是一个ListView或一个Layout或其它须要刷新的组件。

【Android v4包中的 SwipeRefreshLayout 官方的下拉刷新组件】
setOnRefreshListener 用于监听刷新的动作。SwipeRefreshLayout 下拉。就会有刷新的效果出来,触发该监听。
假设须要一个刷新的动画,setRefreshing(true), 停: setRefreshing(false)
假设要禁用刷新动画和手势响应,ssetEnable(false)。    恢复setEnable(true)


来个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< String> arrayAdapter = new ArrayAdapter< String> (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


< android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipe_refresh_widget" android:layout_width="match_parent" android:layout_height="match_parent" > < !-- some full screen pullable view that will be the offsetable content --> < ListView android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" /> < /android.support.v4.widget.SwipeRefreshLayout>






 








    推荐阅读