SwipeRefreshLayout 使用
布局文件:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/id_swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/id_listView"
android:layout_width="match_parent"
android:layout_height="match_parent>
</ListView>
</android.support.v4.widget.SwipeReuhLayot>
主要方法
setOnRefreshListener()
- Set the listener to be notified when a refresh is triggered via the swipe
- 当滑动的时候调用这个监听
setColorScheme()
- Set the color resources used in the progress animation from color resources
- 设置滑动动作发生的时候的颜色源。
isRefreshing()
- Whether the SwipeRefreshWidget is actively showing refresh progress.
- 显示是否刷新进度条
实现过程
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 1:
String test = (String) msg.obj;
mDatas.add(test+i++);
mAdapter.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
break;
default:
break;
}
}
};
这里使用Handler机制是因为子线程不能刷新UI
protected void OnCreate(){
swipeRefreshLayout =(SwipeRefreshLayout) findViewById(R.id.id_swipe);
listView =(ListView) findViewById(R.id.id_listView);
//刷新监听事件
swipeRefreshLayout.setOnRefreshListener(this);
mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mDatas);
listView.setAdapter(mAdapter);
}
//执行刷新动作
@overrid
public void onRefreshListener(){
Message msg= new Message();
msg.what = 1;
msg.obj = "TEST";
handler.sendMessage(msg);
}
主要就是一个刷新动作的监听,然后具体实现刷新的动作