实现:
使用HeaderAndFooterRecyclerView添加Footer实现上拉加载。
1.compile
compile 'com.takwolf.android:hf-recyclerview:0.1.1'
2.使用
使用方法与RecylerView 相同,只是多了两个方法:addHeader /addFooter。
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/com_bg_gray_eeeeee"
android:orientation="vertical">
<com.takwolf.android.hfrecyclerview.HeaderAndFooterRecyclerView
android:id="@+id/acty_allproduct_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
LinearLayout>
java
rv = (HeaderAndFooterRecyclerView) findViewById(R.id.acty_allproduct_rv);
rv.setLayoutManager(new LinearLayoutManager(this));
adapter = new AllProductAdapter(AllProductActivity.this, list);
rv.setAdapter(adapter);
rv.addOnScrollListener(new LoadMore.ForRecyclerView(AllProductActivity.this));
LoadMore
辅助类,实现滑动到底部,回调OnScrollListentrer接口中的方法
public class LoadMore {
private LoadMore(){
}
public static class ForRecyclerView extends RecyclerView.OnScrollListener{
private int mLastVisiblePostion=-1;
private OnLoadMoreListener loadMoreListener;
public ForRecyclerView(OnLoadMoreListener loadMoreListener) {
this.loadMoreListener = loadMoreListener;
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
View firstView =recyclerView.getChildAt(0);
int top=firstView.getTop();
int topEdge=recyclerView.getPaddingTop();
//判断RecyclerView 的itemView是否满屏,如果不满上啦不会触发加载更多
boolean isFullScreen=topint itemCount=manager.getItemCount();
if (newState==RecyclerView.SCROLL_STATE_IDLE&&mLastVisiblePostion==(itemCount-1)&&isFullScreen){
//最后一个item的时候,显示加载更多,
//添加LoadMore布局
loadMoreListener.showLoadMore();
loadMoreListener.onLoadMore();
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
mLastVisiblePostion = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
} else if (layoutManager instanceof GridLayoutManager) {
mLastVisiblePostion = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
int[] lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
mLastVisiblePostion = findMax(lastPositions);
}
}
}
public interface OnLoadMoreListener {
void showLoadMore();
void onLoadMore();
void hideLoadMore();
}
/**
* 获取数组中的最大值
*
* @param lastPositions
* @return
*/
protected static int findMax(int[] lastPositions) {
int max = lastPositions[0];
for (int value : lastPositions) {
if (value > max) {
max = value;
}
}
return max;
}
}
Activity全部代码
public class AllProductActivity extends DrawerBaseActivity implements LoadMore.OnLoadMoreListener {
private HeaderAndFooterRecyclerView rv;
private List list;
private AllProductAdapter adapter;
private View load_more;
private boolean loadMore=false;
private boolean loadMoreFinish=true;
@Override
protected int getLayoutId() {
return R.layout.activity_allproduct;
}
@Override
protected void initView() {
rv = (HeaderAndFooterRecyclerView) findViewById(R.id.acty_allproduct_rv);
rv.setLayoutManager(new LinearLayoutManager(this));
load_more = LayoutInflater.from(this).inflate(R.layout.load_more, null);
//给控件设置弹性效果
OverScrollDecoratorHelper.setUpOverScroll(rv, OverScrollDecoratorHelper.ORIENTATION_VERTICAL);
}
@Override
public void initData() {
list = new ArrayList<>();
QpRetrofitManager.getInstance().getProductAll().subscribe(new BaseObServer() {
@Override
public void onHandleSuccess(ProductData productData) {
if (Util.isNull(productData)||Util.isNull(productData.getBankProduct())||productData.getBankProduct().size()==0){
}else {
list.addAll(productData.getBankProduct());
list.addAll(productData.getBankProduct());
list.addAll(productData.getBankProduct());
list.addAll(productData.getBankProduct());
list.addAll(productData.getBankProduct());
list.addAll(productData.getBankProduct());
list.addAll(productData.getBankProduct());
list.addAll(productData.getBankProduct());
list.addAll(productData.getBankProduct());
list.addAll(productData.getBankProduct());
list.addAll(productData.getBankProduct());
list.addAll(productData.getBankProduct());
list.addAll(productData.getBankProduct());
list.addAll(productData.getBankProduct());
list.addAll(productData.getBankProduct());
adapter = new AllProductAdapter(AllProductActivity.this, list);
rv.setAdapter(adapter);
rv.addOnScrollListener(new LoadMore.ForRecyclerView(AllProductActivity.this));
}
}
});
}
@Override
protected void initListener() {
//让RecyclerView 有弹性效果
// OverScrollDecoratorHelper.setUpOverScroll(rv, OverScrollDecoratorHelper.ORIENTATION_VERTICAL);
}
public static void actionStart(Context context) {
Intent intent = new Intent(context, AllProductActivity.class);
context.startActivity(intent);
}
@Override
public void showLoadMore() {
LogUtils.e("显示加载更多");
ViewGroup parent = (ViewGroup) load_more.getParent();
if (parent != null) {
parent.removeView(load_more);
}
rv.addFooterView(load_more);
ViewGroup.LayoutParams params = load_more.getLayoutParams();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = ViewGroup.LayoutParams.MATCH_PARENT;
load_more.setLayoutParams(params);
}
@Override
public void onLoadMore() {
LogUtils.e("加载更多");
if (!loadMoreFinish) return;
if (isLoadMore()){
//不显示加载更多
hideLoadMore();
}else {
//判读是否剩下的为0
if (Util.isNull(list) || list.size() == 0) {
rv.postDelayed(new Runnable() {
@Override
public void run() {
hideLoadMore();
//没有更多了布局
// setNoMoreView();
loadMore = false;
loadMoreFinish = true;
}
}, 2000);
} else if (list.size() < 10) {
rv.postDelayed(new Runnable() {
@Override
public void run() {
hideLoadMore();
//上拉加载添加评论
adapter.addReplyListAndNotify(list);
//没有更多了布局
// setNoMoreView();
loadMore = false;
loadMoreFinish = true;
}
}, 2000);
} else {
rv.postDelayed(new Runnable() {
@Override
public void run() {
hideLoadMore();
//上拉加载添加评论
adapter.addReplyListAndNotify(list);
loadMoreFinish = true;
}
}, 2000);
}
}
}
@Override
public void hideLoadMore() {
rv.removeFooterView(load_more);
}
public boolean isLoadMore() {
return loadMore;
}
}
Demo 地址
以上便可以实现上拉加载的效果,上拉加载布局可以自定义。