RecyclerView上拉加载更多,下拉刷新

xiaoxiao2021-02-28  28

1:这个是自定义的view,实现了RecyclerView 功能,package com.charles.refreshloadrecyclerview;import android.content.Context;import android.support.v4.widget.SwipeRefreshLayout;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;import static com.charles.refreshloadrecyclerview.SwipeAdapter.NO_MORE;/** * Created by C_harles on 2017/11/15. */public class SwipeRecyclerView extends SwipeRefreshLayout implements SwipeRefreshLayout.OnRefreshListener { private RecyclerView recyclerView; private OnLoadRefreshListener onLoadRefreshListener; private boolean isRefresh; private boolean isLoad; private SwipeAdapter swipeAdapter; private LinearLayoutManager linearLayoutManager; public SwipeRecyclerView(Context context) { super(context); this.init(); } public SwipeRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); this.init(); } public void setOnLoadRefreshListener(OnLoadRefreshListener onLoadRefreshListener) { this.onLoadRefreshListener = onLoadRefreshListener; this.recyclerView.addOnScrollListener(new EndLessOnScrollListener(onLoadRefreshListener, this)); } private void init() { this.recyclerView = new RecyclerView(this.getContext()); this.addView(this.recyclerView); this.setOnRefreshListener(this); this.setRefresh(isRefresh); this.linearLayoutManager = new LinearLayoutManager(getContext()) { public RecyclerView.LayoutParams generateDefaultLayoutParams() { return new RecyclerView.LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } }; this.recyclerView.setLayoutManager(this.linearLayoutManager); } public LinearLayoutManager getLinearLayoutManager() { return linearLayoutManager; } /** * 刷新完成 */ public void stopRefresh() { if (swipeAdapter == null) return; this.isRefresh = false; this.swipeAdapter.viewType = NO_MORE; this.setRefreshing(this.isRefresh); } /** * 加载完成 */ public void stopLoad() { if (swipeAdapter == null) return; this.isLoad = false; this.swipeAdapter.viewType = NO_MORE; this.setEnabled(!this.isLoad); ;//可以刷新 } public void setRefresh(boolean refresh) { this.isRefresh = refresh; this.setEnabled(this.isRefresh); this.setRefreshing(this.isRefresh); if (!isLoad && isRefresh && onLoadRefreshListener != null) onLoadRefreshListener.onRefresh(); } public void setLoad(boolean load) { this.isLoad = load; if (this.isLoad) {//禁止刷新 this.setRefresh(false); } } public boolean isLoad() { return isLoad; } public void setAdapter(RecyclerView.Adapter adapter) { if (adapter instanceof SwipeAdapter) { swipeAdapter = (SwipeAdapter) adapter; swipeAdapter.setSwipeRecyclerView(this); } this.recyclerView.setAdapter(adapter); } @Override public void onRefresh() { if (onLoadRefreshListener != null) { this.setRefresh(true); onLoadRefreshListener.onRefresh(); } } public SwipeAdapter getSwipeAdapter() { return swipeAdapter; } public RecyclerView getRecyclerView() { return recyclerView; } }2:自定义适配器package com.charles.refreshloadrecyclerview; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by C_harles on 2017/11/15. */ public abstract class SwipeAdapter<VH extends ViewHolder> extends RecyclerView.Adapter<VH> { //上拉加载更多 static final int PULL_LOAD_MORE = 10000; //正在加载更多 static final int LOADING_MORE = 10001; //没有更多 static final int NO_MORE = 10002; int viewType = NO_MORE; private SwipeRecyclerView swipeRecyclerView; public void setSwipeRecyclerView(SwipeRecyclerView swipeRecyclerView) { this.swipeRecyclerView = swipeRecyclerView; } @Override public final VH onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == PULL_LOAD_MORE || viewType == LOADING_MORE) return (VH) new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_layout, null)); else return onCreateViewHolders(parent, viewType); } @Override public final int getItemViewType(int position) { if (swipeRecyclerView != null) { if (getItemCounts() == position) return viewType; } return getItemViewTypes(position); } public int getItemViewTypes(int position) { return super.getItemViewType(position); } @Override public final int getItemCount() { if (swipeRecyclerView != null) { if (viewType == PULL_LOAD_MORE || viewType == LOADING_MORE) { return getItemCounts() + 1; } } return getItemCounts(); } public abstract int getItemCounts(); public abstract VH onCreateViewHolders(ViewGroup parent, int viewType); @Override public final void onBindViewHolder(VH holder, int position) { int type = getItemViewType(position); if (type == PULL_LOAD_MORE) { holder.setProgressBarShow(View.GONE); holder.setTextViewText("上拉加载更多"); } else if (type == LOADING_MORE) { holder.setProgressBarShow(View.VISIBLE); holder.setTextViewText("正在加载更多"); } else { this.onBindViewHolders(holder, position); } } public abstract void onBindViewHolders(VH holder, int position); } 3:自定义 ViewHaodler package com.charles.refreshloadrecyclerview; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; /** * Created by C_harles on 2017/11/15. */ public class ViewHolder extends RecyclerView.ViewHolder { private ProgressBar progressBar; private TextView textView; public ViewHolder(View itemView) { super(itemView); this.progressBar = (ProgressBar) itemView.findViewById(R.id.progress); this.textView = (TextView) itemView.findViewById(R.id.textShow); } public final void setProgressBarShow(int visibility) { if (this.progressBar != null) this.progressBar.setVisibility(visibility); } public final void setTextViewText(Character character) { if (this.textView != null) this.textView.setText(character); } public final void setTextViewText(String character) { if (this.textView != null) this.textView.setText(character); } } 4:刷新接口package com.charles.refreshloadrecyclerview; /** * Created by C_harles on 2017/11/16. */ public interface OnLoadRefreshListener { void onRefresh(); void onLoad(); } 5:这块是回调加载更多package com.charles.refreshloadrecyclerview; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import static com.charles.refreshloadrecyclerview.SwipeAdapter.LOADING_MORE; import static com.charles.refreshloadrecyclerview.SwipeAdapter.NO_MORE; import static com.charles.refreshloadrecyclerview.SwipeAdapter.PULL_LOAD_MORE; /** * Created by C_harles on 2017/11/16. */ public class EndLessOnScrollListener extends RecyclerView.OnScrollListener { private OnLoadRefreshListener onLoadRefreshListener; private SwipeRecyclerView swipeRecyclerView; private LinearLayoutManager linearLayoutManager; private SwipeAdapter adapter; private int dy; public EndLessOnScrollListener(OnLoadRefreshListener onLoadRefreshListener, SwipeRecyclerView swipeRecyclerView) { this.onLoadRefreshListener = onLoadRefreshListener; this.swipeRecyclerView = swipeRecyclerView; if (swipeRecyclerView == null) return; this.adapter = this.swipeRecyclerView.getSwipeAdapter(); this.linearLayoutManager = swipeRecyclerView.getLinearLayoutManager(); } @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState != RecyclerView.SCROLL_STATE_IDLE)//不动时 return; if (swipeRecyclerView == null || linearLayoutManager == null || adapter == null) return; if (this.swipeRecyclerView.isRefreshing())//正在刷新的话禁止加载更多 return; if (linearLayoutManager.findLastVisibleItemPosition() + 1 < adapter.getItemCount()) return; // if (linearLayoutManager.getChildCount() == adapter.getItemCount()) { // Log.i("sss","----------------"); // return; // } if (onLoadRefreshListener != null && !swipeRecyclerView.isLoad()) { if (adapter.viewType == NO_MORE) {//上拉加载更多 adapter.viewType = PULL_LOAD_MORE; adapter.notifyDataSetChanged(); } else if (adapter.viewType == PULL_LOAD_MORE) {//加载更多 adapter.viewType = LOADING_MORE; swipeRecyclerView.setLoad(true); onLoadRefreshListener.onLoad(); adapter.notifyDataSetChanged(); }//正在在加载 } } }6:加载更多的布局文件<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="horizontal" android:padding="10dp"><ProgressBar android:id="@+id/progress" android:layout_width="18dp" android:layout_height="18dp" /> <TextView android:id="@+id/textShow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="正在加载" android:textColor="#999999" android:textSize="16sp" /> </LinearLayout>
转载请注明原文地址: https://www.6miu.com/read-1950372.html

最新回复(0)