因为利用系统控件,实现非常简单,所以直接进入主题,下面是布局文件:
<view
android:id="@+id/swipeRefreshLayout"
class="android.support.v4.widget.SwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</view>
这里被SwipeRefreshLayout 包裹的布局,就可以使用下拉刷新,
Activity操作:
private SwipeRefreshLayout swipeRefreshLayout;
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setColorSchemeColors(Color.RED);//这里设置刷新图标颜色
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//重新获取数据
//获取完成swipeRefreshLayout.setRefreshing(false);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);//为了模拟刷新,2秒后刷新完成。
handler.sendEmptyMessage(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
});
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
swipeRefreshLayout.setRefreshing(false);
}
};
```