查看: 1336|回复: 0

[手机开发] Android RecyclerView上拉加载和下拉刷新

发表于 2017-8-1 08:00:04

之前写过一篇刷新加载《RecyclerView上拉加载和下拉刷新(基础版)》 ,这次是进行改装完善。

代码中注释的很详细,所以就直接上代码了。

核心实现

  1. package com.example.fly.recyclerviewrefresh.base;
  2. import android.os.Bundle;
  3. import android.support.annotation.Nullable;
  4. import android.support.v4.widget.SwipeRefreshLayout;
  5. import android.support.v7.widget.LinearLayoutManager;
  6. import android.support.v7.widget.RecyclerView;
  7. import com.example.fly.recyclerviewrefresh.R;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. /**
  11. * 类名:
  12. * 类描述:下拉刷新和上拉加载
  13. * 创建人:fly
  14. * 创建日期: 2017/2/2.
  15. * 版本:V1.0
  16. */
  17. public abstract class RefreshActivity<T extends Object,A extends RecyclerView.Adapter> extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
  18. protected SwipeRefreshLayout swipeRefreshLayout;
  19. protected RecyclerView recyclerView;
  20. protected LinearLayoutManager linearLayoutManager;
  21. protected A adapter;
  22. protected List<T> lists = new ArrayList<>();
  23. protected T dataBeanLoadMore = createBean();
  24. protected boolean isLoadDataIng; // 默认为false 是否正在加载数据, false 不在加载, true 正在加载
  25. protected boolean isScrollY; // 是否在Y轴(垂直方向)上滑动, false 没滑动, true 滑动
  26. @Override
  27. protected void onCreate(@Nullable Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(setLayout());
  30. swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
  31. swipeRefreshLayout.setOnRefreshListener(this);
  32. swipeRefreshLayout.setColorSchemeResources(Refresh.colors); // 设置变化的颜色
  33. recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
  34. linearLayoutManager = new LinearLayoutManager(this);
  35. recyclerView.setLayoutManager(linearLayoutManager);
  36. loadMore();
  37. }
  38. /**作用就是能在父类中初始化公共的控件如上面的onCreate(@Nullable Bundle savedInstanceState)初始化的控件*/
  39. protected abstract int setLayout();
  40. public abstract T createBean();
  41. // public abstract A createAdapter();
  42. /**上拉加载*/
  43. protected void loadMore() {
  44. recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
  45. /**
  46. *
  47. * @param recyclerView 当前在滚动的RecyclerView
  48. * @param newState 当前滚动状态
  49. * newState有三种值:
  50. //静止,没有滚动
  51. public static final int SCROLL_STATE_IDLE = 0;
  52. //正在被外部拖拽,一般为用户正在用手指滚动
  53. public static final int SCROLL_STATE_DRAGGING = 1;
  54. //自动滚动开始
  55. public static final int SCROLL_STATE_SETTLING = 2;
  56. */
  57. @Override
  58. public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  59. super.onScrollStateChanged(recyclerView, newState);
  60. if (newState == RecyclerView.SCROLL_STATE_IDLE && isLoadDataIng == false ) {
  61. int lastVisibleItem = linearLayoutManager.findLastCompletelyVisibleItemPosition(); // 获取最后一个item的角标
  62. int totalItemCount = linearLayoutManager.getItemCount(); // 获取item的总数
  63. if (lastVisibleItem == (totalItemCount - 1)) { // 判断滑到最后一个item
  64. if (!swipeRefreshLayout.isRefreshing()) { //不再下拉刷新状态
  65. if (isScrollY) { // 在Y轴(垂直方向)上有滑动
  66. // TODO 执行上拉加载
  67. onShowRefresh();
  68. loadMoreData();
  69. }
  70. }
  71. }
  72. }
  73. }
  74. /**
  75. *
  76. * @param recyclerView 当前滚动的view
  77. * @param dx 水平滚动距离
  78. * @param dy 垂直滚动距离
  79. */
  80. @Override
  81. public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  82. super.onScrolled(recyclerView, dx, dy);
  83. if (dy > 0) {
  84. isScrollY = true;
  85. }else{
  86. isScrollY = false;
  87. }
  88. }
  89. });
  90. }
  91. /**加载数据*/
  92. public abstract void loadMoreData();
  93. /**显示加在更多的View*/
  94. protected void onShowRefresh() {
  95. isLoadDataIng = true; // 正在加载数据
  96. lists.add(dataBeanLoadMore);
  97. adapter.notifyDataSetChanged();
  98. }
  99. /**隐藏加在更多的View*/
  100. protected void onHintLoadMore() {
  101. lists.remove(dataBeanLoadMore);
  102. isLoadDataIng = false; // 不在加载数据
  103. adapter.notifyDataSetChanged();
  104. }
  105. }
复制代码
  1. package com.example.fly.recyclerviewrefresh;
  2. import android.os.Handler;
  3. import android.os.Bundle;
  4. import com.example.fly.recyclerviewrefresh.adapter.RecyclerAdapter;
  5. import com.example.fly.recyclerviewrefresh.base.Refresh;
  6. import com.example.fly.recyclerviewrefresh.base.RefreshActivity;
  7. import com.example.fly.recyclerviewrefresh.bean.DataBean;
  8. /**
  9. * 类名:
  10. * 类描述:RecyclerView刷新 方法二
  11. * 创建人:fly
  12. * 创建日期: 2017/2/2.
  13. * 版本:V1.0
  14. */
  15. public class MainActivity extends RefreshActivity<DataBean,RecyclerAdapter>{
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. for (int i = 0; i < 20; i++) {
  20. DataBean dataBean = new DataBean(Refresh.COMMON);
  21. dataBean.number = String.valueOf(i);
  22. lists.add(dataBean);
  23. }
  24. adapter = new RecyclerAdapter(lists,this);
  25. recyclerView.setAdapter(adapter);
  26. }
  27. @Override
  28. public void onRefresh() {
  29. new Handler().postDelayed(new Runnable() {
  30. @Override
  31. public void run() {
  32. lists.clear();
  33. adapter.notifyDataSetChanged();
  34. swipeRefreshLayout.setRefreshing(false);
  35. }
  36. },1000);
  37. }
  38. @Override
  39. protected int setLayout() {return R.layout.activity_main;}
  40. @Override
  41. public DataBean createBean() {return new DataBean(Refresh.LOAD_MORE);}
  42. @Override
  43. public void loadMoreData() {
  44. for (int i = 0; i < 5; i++) {
  45. DataBean dataBean = new DataBean(Refresh.COMMON);
  46. dataBean.number = String.valueOf(i) + "fly";
  47. lists.add(dataBean);
  48. }
  49. adapter.notifyDataSetChanged();
  50. onHintLoadMore();
  51. }
  52. }
复制代码

源码下载:RecyclerViewRefresh刷新加载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报

关闭

站长推荐上一条 /1 下一条