之前写过一篇刷新加载《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;
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();
}
protected abstract int setLayout();
public abstract T createBean();
// public abstract A createAdapter();
protected void loadMore() {
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@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();
}
}
}
}
}
@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();
protected void onShowRefresh() {
isLoadDataIng = true; // 正在加载数据
lists.add(dataBeanLoadMore);
adapter.notifyDataSetChanged();
}
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;
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刷新加载
您可能感兴趣的文章:android RecyclerView侧滑菜单,滑动删除,长按拖拽,下拉刷新上拉加载Android RecyclerView实现下拉刷新和上拉加载更多Android之RecyclerView轻松实现下拉刷新和加载更多示例Android RecyclerView上拉加载和下拉刷新(基础版)RecyclerView下拉刷新上拉加载Android RecyclerView下拉刷新和上拉加载更多Android使用recyclerview打造真正的下拉刷新上拉加载效果Android RecyclerView 上拉加载更多及下拉刷新功能的实现方法Android RecyclerView实现下拉刷新和上拉加载XRecyclerView实现下拉刷新、滚动到底部加载更多等功能