欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android RecyclerView上拉加载和下拉刷新

程序员文章站 2023-12-19 09:38:46
之前写过一篇刷新加载《recyclerview上拉加载和下拉刷新(基础版)》 ,这次是进行改装完善。 代码中注释的很详细,所以就直接上代码了。 核心实现 pa...

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

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

核心实现

package com.example.fly.recyclerviewrefresh.base;

import android.os.bundle;
import android.support.annotation.nullable;
import android.support.v4.widget.swiperefreshlayout;
import android.support.v7.widget.linearlayoutmanager;
import android.support.v7.widget.recyclerview;
import com.example.fly.recyclerviewrefresh.r;
import java.util.arraylist;
import java.util.list;

/**
 * 类名:
 * 类描述:下拉刷新和上拉加载
 * 创建人:fly
 * 创建日期: 2017/2/2.
 * 版本:v1.0
 */
public abstract class refreshactivity<t extends object,a extends recyclerview.adapter> extends baseactivity implements swiperefreshlayout.onrefreshlistener {
 protected swiperefreshlayout swiperefreshlayout;
 protected recyclerview recyclerview;
 protected linearlayoutmanager linearlayoutmanager;
 protected a adapter;
 protected list<t> lists = new arraylist<>();
 protected t databeanloadmore = createbean();
 protected boolean isloaddataing; // 默认为false 是否正在加载数据, false 不在加载, true 正在加载
 protected boolean isscrolly; // 是否在y轴(垂直方向)上滑动, false 没滑动, true 滑动

 @override
 protected void oncreate(@nullable bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(setlayout());
 swiperefreshlayout = (swiperefreshlayout) findviewbyid(r.id.swiperefreshlayout);
 swiperefreshlayout.setonrefreshlistener(this);
 swiperefreshlayout.setcolorschemeresources(refresh.colors); // 设置变化的颜色
 recyclerview = (recyclerview) findviewbyid(r.id.recyclerview);
 linearlayoutmanager = new linearlayoutmanager(this);
 recyclerview.setlayoutmanager(linearlayoutmanager);
 loadmore();
 }

 /**作用就是能在父类中初始化公共的控件如上面的oncreate(@nullable bundle savedinstancestate)初始化的控件*/
 protected abstract int setlayout();

 public abstract t createbean();

// public abstract a createadapter();

 /**上拉加载*/
 protected void loadmore() {
 recyclerview.addonscrolllistener(new recyclerview.onscrolllistener() {

 /**
 *
 * @param recyclerview 当前在滚动的recyclerview
 * @param newstate 当前滚动状态
 *  newstate有三种值:
 //静止,没有滚动
 public static final int scroll_state_idle = 0;

 //正在被外部拖拽,一般为用户正在用手指滚动
 public static final int scroll_state_dragging = 1;

 //自动滚动开始
 public static final int scroll_state_settling = 2;
 */
 @override
 public void onscrollstatechanged(recyclerview recyclerview, int newstate) {
 super.onscrollstatechanged(recyclerview, newstate);
 if (newstate == recyclerview.scroll_state_idle && isloaddataing == false ) {
  int lastvisibleitem = linearlayoutmanager.findlastcompletelyvisibleitemposition(); // 获取最后一个item的角标
  int totalitemcount = linearlayoutmanager.getitemcount(); // 获取item的总数
  if (lastvisibleitem == (totalitemcount - 1)) { // 判断滑到最后一个item
  if (!swiperefreshlayout.isrefreshing()) { //不再下拉刷新状态
  if (isscrolly) { // 在y轴(垂直方向)上有滑动
  // todo 执行上拉加载
  onshowrefresh();
  loadmoredata();
  }
  }
  }
 }
 }

 /**
 *
 * @param recyclerview 当前滚动的view
 * @param dx 水平滚动距离
 * @param dy 垂直滚动距离
 */
 @override
 public void onscrolled(recyclerview recyclerview, int dx, int dy) {
 super.onscrolled(recyclerview, dx, dy);
 if (dy > 0) {
  isscrolly = true;
 }else{
  isscrolly = false;
 }
 }
 });
 }

 /**加载数据*/
 public abstract void loadmoredata();

 /**显示加在更多的view*/
 protected void onshowrefresh() {
 isloaddataing = true; // 正在加载数据
 lists.add(databeanloadmore);
 adapter.notifydatasetchanged();
 }

 /**隐藏加在更多的view*/
 protected void onhintloadmore() {
 lists.remove(databeanloadmore);
 isloaddataing = false; // 不在加载数据
 adapter.notifydatasetchanged();
 }
}

package com.example.fly.recyclerviewrefresh;

import android.os.handler;
import android.os.bundle;
import com.example.fly.recyclerviewrefresh.adapter.recycleradapter;
import com.example.fly.recyclerviewrefresh.base.refresh;
import com.example.fly.recyclerviewrefresh.base.refreshactivity;
import com.example.fly.recyclerviewrefresh.bean.databean;

/**
 * 类名:
 * 类描述:recyclerview刷新 方法二
 * 创建人:fly
 * 创建日期: 2017/2/2.
 * 版本:v1.0
 */
public class mainactivity extends refreshactivity<databean,recycleradapter>{

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 for (int i = 0; i < 20; i++) {
 databean databean = new databean(refresh.common);
 databean.number = string.valueof(i);
 lists.add(databean);
 }
 adapter = new recycleradapter(lists,this);
 recyclerview.setadapter(adapter);
 }

 @override
 public void onrefresh() {
 new handler().postdelayed(new runnable() {
 @override
 public void run() {
 lists.clear();
 adapter.notifydatasetchanged();
 swiperefreshlayout.setrefreshing(false);
 }
 },1000);
 }

 @override
 protected int setlayout() {return r.layout.activity_main;}

 @override
 public databean createbean() {return new databean(refresh.load_more);}

 @override
 public void loadmoredata() {
 for (int i = 0; i < 5; i++) {
 databean databean = new databean(refresh.common);
 databean.number = string.valueof(i) + "fly";
 lists.add(databean);
 }
 adapter.notifydatasetchanged();
 onhintloadmore();
 }
}

源码下载:recyclerviewrefresh刷新加载

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

上一篇:

下一篇: