android 5.0 后google終于在 support v4 包下 增加了下拉刷新的控件項(xiàng)目地址: https://github.com/stormzhang/SwipeRefreshLayoutDemo
191337005943568.gif
xml:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp" />
</android.support.v4.widget.SwipeRefreshLayout>
java實(shí)現(xiàn)監(jiān)聽:
implements SwipeRefreshLayout.OnRefreshListener
/*
* 監(jiān)聽器SwipeRefreshLayout.OnRefreshListener中的方法浅侨,當(dāng)下拉刷新后觸發(fā)
*/
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// 停止刷新
mSwipeLayout.setRefreshing(false);
}
}, 5000); // 5秒后發(fā)送消息溃列,停止刷新
}
其他方法:
初始化下拉刷新控件
mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
mSwipeLayout.setOnRefreshListener(this);
// 設(shè)置下拉圓圈上的顏色脉幢,藍(lán)色、綠色缎谷、橙色、紅色
mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
android.R.color.holo_orange_light, android.R.color.holo_red_light);
mSwipeLayout.setDistanceToTriggerSync(400);// 設(shè)置手指在屏幕下拉多少距離會(huì)觸發(fā)下拉刷新
mSwipeLayout.setProgressBackgroundColor(R.color.red); // 設(shè)定下拉圓圈的背景
mSwipeLayout.setSize(SwipeRefreshLayout.LARGE); // 設(shè)置圓圈的大小