时间:2020-10-30 16:29:49 | 栏目:Android代码 | 点击:次
之前写过一篇刷新加载《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(); } }