SwipeRefreshLayout 使用
写在前面:最近在研究下拉刷新,虽然自己也实践写出来了一个Demo,
但是还是在github上找到了一个刷新的框架
框架地址:SwipeRefreshLayout
布局文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <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.
显示是否刷新进度条
##实现过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 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); }
|
主要就是一个刷新动作的监听,然后具体实现刷新的动作