概要
试了很多第三方的下拉刷新不是效果不好看 就是有bug,最后还是决定用官方的下拉刷新,但是官方的默认不支持进入页面立即刷新,所以我们可以用官方的并对其扩展
官方原版的用法
XML
1 2 3 4 5 6 7 8 9 10 11 12
| <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/id_swipe_ly" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/file_list" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@color/zj_qianhuise" android:dividerHeight="1px"> </ListView> </android.support.v4.widget.SwipeRefreshLayout>
|
代码中
1 2
| private SwipeRefreshLayout mSwipeLayout;
|
实现SwipeRefreshLayout.OnRefreshListener接口
添加回调方法
1 2 3 4
| @Override public void onRefresh() { loadData(); }
|
初始化
1 2 3 4 5 6 7
| mSwipeLayout = (SwipeRefreshLayout) parentView.findViewById(R.id.id_swipe_ly); mSwipeLayout.setOnRefreshListener(this);
mSwipeLayout.setProgressBackgroundColorSchemeColor(getResources().getColor(android.R.color.background_light));
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);
|
如上loadData()来加载数据,加载结束后记得调用下面的方法停止刷新动画
1
| mSwipeLayout.setRefreshing(false);
|
进入页面立即刷新
但是我们想做到一进页面就立刻刷新,并有刷新动画怎么办
首先添加一个类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public class AutoSwipeRefreshLayout extends SwipeRefreshLayout {
public AutoSwipeRefreshLayout(Context context) { super(context); }
public AutoSwipeRefreshLayout(Context context, AttributeSet attrs) { super(context, attrs); }
public void autoRefresh() { try { Field mCircleView = SwipeRefreshLayout.class.getDeclaredField("mCircleView"); mCircleView.setAccessible(true); View progress = (View) mCircleView.get(this); progress.setVisibility(VISIBLE);
Method setRefreshing = SwipeRefreshLayout.class.getDeclaredMethod("setRefreshing", boolean.class, boolean.class); setRefreshing.setAccessible(true); setRefreshing.invoke(this, true, true); } catch (Exception e) { e.printStackTrace(); } } }
|
然后XML改为
1 2 3 4 5 6 7 8 9 10 11 12
| <cn.psvmc.swiperefreshlayout.AutoSwipeRefreshLayout android:id="@+id/id_swipe_ly" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/file_list" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@color/zj_qianhuise" android:dividerHeight="1px"> </ListView> </cn.psvmc.swiperefreshlayout.AutoSwipeRefreshLayout>
|
对应的java代码中的SwipeRefreshLayout 都换成 AutoSwipeRefreshLayout
页面加载后调用mSwipeLayout.autoRefresh()就可以了
但是立即执行mSwipeLayout.autoRefresh()在效果上不是很流畅 所以我用了消息机制 延迟发送消息 就好了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| interface ZJHandlerStatus { int endrefresh = 0; int endDialog = 1; int reloadData = 1; int autoRefresh = 2; }
Handler myHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case ZJHandlerStatus.endrefresh: mSwipeLayout.setRefreshing(false); break; case ZJHandlerStatus.reloadData: fileAdapter.notifyDataSetChanged(); break; case ZJHandlerStatus.autoRefresh: mSwipeLayout.autoRefresh(); break; } super.handleMessage(msg); } };
Message message = new Message(); message.what = ZJHandlerStatus.autoRefresh; myHandler.sendMessageAtTime(message, SystemClock.uptimeMillis() + 600);
|