MaterialDesign--下拉刷新控件--SwipeRefreshLayout

淩亂°似流年 2022-07-18 08:40 297阅读 0赞

前言:

从今天开始,学习和记录一个学习google自发布MaterialDesign风格而加入的新控件.今天是看的SwipeRefreshLayout,一个下拉刷新控件.现在很多app上都用的是这个,网易,知乎等都用上了,MaterialDesign风格标准化真的是大势所趋.先看一下网易的效果:

Center

需求分析:

今天主要是想实现一下几个功能

1.手势下拉进入刷新状态,出现刷新的圆形箭头,开始刷新数据,数据刷新结束后隐藏

2.进入页面的时候,自动刷新数据,刷新结束后隐藏:

开始使用:

1.布局上声明使用:

这里要注意的地方是swipeRefreshLayout是一个特殊的布局,只允许有一个直接子控件.

  1. <android.support.v4.widget.SwipeRefreshLayout
  2. android:id="@+id/swipeRefreshLayout"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ListView
  6. android:id="@+id/listView"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content">
  9. </ListView>
  10. </android.support.v4.widget.SwipeRefreshLayout>

2.手动下拉刷新功能实现:

activity绑定数据,设置刷新监听:

在onRefresh()里面做相应刷新数据的操作,我这里为了测试,就只是做的空倒计时后关闭刷新

  1. swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
  2. swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  3. @Override
  4. public void onRefresh() {//正在刷新
  5. new CountDownTimer(3000, 500) {
  6. @Override
  7. public void onTick(long millisUntilFinished) {
  8. }
  9. @Override
  10. public void onFinish() {
  11. //刷新结束
  12. swipeRefreshLayout.setRefreshing(false);
  13. }
  14. }.start();
  15. }
  16. });

3.自动下拉刷新功能实现:

思路是刚进入页面的时候,让swipeRefreshLayout.setRefreshing(true),然后走OnRefreshListener监听事件的onRefresh()方法加载刷新数据,一切都是那么刚刚好.

然而试了之后并没有什么用,原因在于swipeRefreshLayout.setRefreshing(true)方法不会触发OnRefreshListener.

解决方法:手动调用OnRefreshListener中的onRefresh()方法

(1)定义OnRefreshListener监听事件,为全局变量

  1. SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
  2. @Override
  3. public void onRefresh() {
  4. new CountDownTimer(3000, 500) {
  5. @Override
  6. public void onTick(long millisUntilFinished) {
  7. }
  8. @Override
  9. public void onFinish() {
  10. //刷新结束
  11. swipeRefreshLayout.setRefreshing(false);
  12. }
  13. }.start();
  14. }
  15. };

(2)根据我们的需要,在onCreate()或者onResum()中手动使swipeRefreshLayout处于刷新状态,并调用OnRefreshListener中的onRefresh()方法

  1. //设置为正在刷新状态
  2. swipeRefreshLayout.setRefreshing(true);
  3. //触发监听的onRefresh()方法
  4. onRefreshListener.onRefresh();

更多了解:

其实想知道更多关于这个控件的使用,最好的方式就是看源码,而且这个控件的源码才1200多行,看起来不会有太大压力,注释写的都很清楚,例如这个类注释:

  1. /**
  2. * The SwipeRefreshLayout should be used whenever the user can refresh the
  3. * contents of a view via a vertical swipe gesture. The activity that
  4. * instantiates this view should add an OnRefreshListener to be notified
  5. * whenever the swipe to refresh gesture is completed. The SwipeRefreshLayout
  6. * will notify the listener each and every time the gesture is completed again;
  7. * the listener is responsible for correctly determining when to actually
  8. * initiate a refresh of its content. If the listener determines there should
  9. * not be a refresh, it must call setRefreshing(false) to cancel any visual
  10. * indication of a refresh. If an activity wishes to show just the progress
  11. * animation, it should call setRefreshing(true). To disable the gesture and
  12. * progress animation, call setEnabled(false) on the view.
  13. * <p>
  14. * This layout should be made the parent of the view that will be refreshed as a
  15. * result of the gesture and can only support one direct child. This view will
  16. * also be made the target of the gesture and will be forced to match both the
  17. * width and the height supplied in this layout. The SwipeRefreshLayout does not
  18. * provide accessibility events; instead, a menu item must be provided to allow
  19. * refresh of the content wherever this gesture is used.
  20. * </p>
  21. */

说明了swipeRefreshLayout的主要用途和使用注意事项,总结一下主要是这几个点:

1.使用OnRefreshListener监听刷新

2.使用setRefreshing(boolean isRegreshing)设置刷新状态

3.使用setEnable(boolean isEnable)设置是否禁用手势下拉刷新

4.只支持一个直接子View,布局要求设置为宽和高都铺满.

还有很多其他的功能,例如设置swipeRefreshLayout的样式等,都可以从源码中找到,从structure窗口查看超级方便,今天太晚了,明天有时间会都测试一下.
Center 1

发表评论

表情:
评论列表 (有 0 条评论,297人围观)

还没有评论,来说两句吧...

相关阅读