博客
关于我
自定义RecyclerView实现下拉刷新和上拉加载
阅读量:234 次
发布时间:2019-02-28

本文共 17074 字,大约阅读时间需要 56 分钟。

通过导入第三方库实现ListView的上拉加载和下拉刷新比较简单,今天我要讲的是自定义RecyclerView实现下拉刷新和上拉加载。首先,自定义下拉刷新上拉加载是通过给RecyclerView添加头部和尾部实现的。而问题是RecyclerView并没有addHeaderView(View v)和addFooterView(View v)方法。

     第一步:自定义HeaderAndFooterWrapper(装饰者模式)实现给RecyclerView添加头部和尾部
    方法:public void addHeaderView(View v)
              public void addFooterView(View v)
     代码参照张鸿洋:Android 优雅的为RecyclerView添加HeaderView和FooterView 
     链接:

      第二步:定义类RefreshRecyclerView并抽取为库

1) 头部布局:refresh_recyclerview_header.xml

 

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <!--下拉刷新控件-->

  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  4. android:layout_width="match_parent"

  5. android:layout_height="wrap_content"

  6. android:gravity="center"

  7. android:orientation="horizontal">

  8.  
  9. <FrameLayout

  10. android:layout_width="wrap_content"

  11. android:layout_height="wrap_content"

  12. android:padding="10dp">

  13.  
  14. <ImageView

  15. android:id="@+id/iv_header_refresh"

  16. android:layout_width="wrap_content"

  17. android:layout_height="wrap_content"

  18. android:layout_gravity="center"

  19. android:src="@drawable/headview_red_arrow" />

  20.  
  21. <ProgressBar

  22. android:id="@+id/pb_header_refresh"

  23. android:layout_width="wrap_content"

  24. android:layout_height="wrap_content"

  25. android:layout_gravity="center"

  26. android:indeterminateDrawable="@drawable/custom_progressbar"

  27. android:visibility="gone" />

  28. </FrameLayout>

  29.  
  30. <LinearLayout

  31. android:layout_width="wrap_content"

  32. android:layout_height="wrap_content"

  33. android:layout_gravity="center_vertical"

  34. android:orientation="vertical">

  35.  
  36. <TextView

  37. android:id="@+id/tv_status"

  38. android:layout_width="match_parent"

  39. android:layout_height="wrap_content"

  40. android:gravity="center_horizontal"

  41. android:text="下拉刷新"

  42. android:textColor="#ff0000"

  43. android:textSize="18sp" />

  44.  
  45. <TextView

  46. android:id="@+id/tv_time"

  47. android:layout_width="match_parent"

  48. android:layout_height="wrap_content"

  49. android:layout_marginTop="5dp"

  50. android:gravity="center_horizontal"

  51. android:text="上次更新时间:2016-10-31"

  52. android:textColor="#55000000"

  53. android:textSize="16sp" />

  54.  
  55. </LinearLayout>

  56.  
  57. </LinearLayout>


 

 

2)尾部布局(上拉加载部分):refresh_recyclerview_footer.xml

 
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:gravity="center"

  5. android:layout_height="wrap_content"

  6. android:orientation="horizontal">

  7. <ProgressBar

  8. android:indeterminateDrawable="@drawable/custom_progressbar"

  9. android:layout_margin="5dp"

  10. android:layout_width="wrap_content"

  11. android:layout_height="wrap_content" />

  12. <TextView

  13. android:text="加载更多中...."

  14. android:textColor="#ff0000"

  15. android:textSize="25sp"

  16. android:layout_marginLeft="10dp"

  17. android:layout_width="wrap_content"

  18. android:layout_height="wrap_content" />

  19. </LinearLayou