Android v4包中的 SwipeRefreshLayout 官方的下拉刷新组件
SwipeRefreshLayout在v4包下,对应的v4Demo中也有相应的例子。如果没有请下载最新support-v4
SwipeRefreshLayout 只能有一个直接子View,可能是一个ListView或一个Layout或其他需要刷新的组件。
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>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。