RecyclerView上拉加载封装代码

RecyclerView上拉加载封装代码,第1张

概述RecyclerView上拉加载,先看效果:网上有很多这类得框架,不过在自己的项目只用到上拉加载的功能,所以自己封装一个简单点的。

RecyclerVIEw上拉加载,先看效果:

网上有很多这类得框架,不过在自己的项目只用到上拉加载的功能,所以自己封装一个简单点的。

主要依赖BaseRecyPRAdapter这类

public abstract class BaseRecyPRAdapter<T> extends RecyclerVIEw.Adapter<BaseRecyPRAdapter.BaseVIEwHolder> {  private static final String TAG = "BaseRecyPRAdapter";  public static final int VIEW_ITEM = 0;  public static final int VIEW_PROG = 1;  public List<T> mDataList;  private final Context mContext;  private final RecyclerVIEw mRecyclerVIEw;  private int totalitemCount;  private int lastVisibleItemposition;  boolean isShowFootVIEW = false;  private Handler handler = new Handler();  private static final int STATE_norMAL = 0;  private static final int STATE_LOADING = 1;  private static final int STATE_LOAD_COMPLETE = 2;  private int mState = STATE_norMAL;  public BaseRecyPRAdapter(Context context,RecyclerVIEw recyclerVIEw) {    mContext = context;    mRecyclerVIEw = recyclerVIEw;    if (mRecyclerVIEw.getLayoutManager() instanceof linearlayoutmanager) {      final linearlayoutmanager linearlayoutmanager = (linearlayoutmanager) recyclerVIEw.getLayoutManager();      mRecyclerVIEw.addOnScrollListener(new RecyclerVIEw.OnScrollListener() {        @OverrIDe        public voID onScrollStateChanged(RecyclerVIEw recyclerVIEw,int newState) {          super.onScrollStateChanged(recyclerVIEw,newState);          int extent = recyclerVIEw.computeVerticalScrollExtent();          int range = recyclerVIEw.computeVerticalScrollRange();          Log.e(TAG,"\n extent = " + extent + "\n range = " + range);          if (range > extent) {            if(!isShowFootVIEW){              notifyDataSetChanged();            }            isShowFootVIEW = true;          } else {            isShowFootVIEW = false;          }          totalitemCount = linearlayoutmanager.getItemCount();          if (mState == STATE_norMAL && newState == RecyclerVIEw.SCRolL_STATE_IDLE &&              totalitemCount == lastVisibleItemposition + 1 && range > extent) {            mState = STATE_LOADING;            handler.post(new Runnable() {              @OverrIDe              public voID run() {                if (mloadMoreDataListener != null) {                  mloadMoreDataListener.loadMoreData();                }              }            });          }        }        @OverrIDe        public voID onScrolled(RecyclerVIEw recyclerVIEw,int dx,int dy) {          super.onScrolled(recyclerVIEw,dx,dy);          lastVisibleItemposition = linearlayoutmanager.findLastVisibleItemposition();        }      });    }  }  @OverrIDe  public BaseVIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) {    BaseVIEwHolder holder = null;    VIEw vIEw = null;    if (vIEwType == VIEW_PROG) {      vIEw = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_footer,parent,false);    } else {      vIEw = LayoutInflater.from(parent.getContext()).inflate(getLayoutID(vIEwType),false);    }    holder = new BaseVIEwHolder(vIEw);    return holder;  }  @OverrIDe  public voID onBindVIEwHolder(BaseVIEwHolder holder,int position) {    if (holder.getItemVIEwType() == BaseRecyPRAdapter.VIEW_PROG) {//      LogTool.LogE_DEBUG(TAG,"onBindVIEwHolder--->" + mState);      Progressbar progressbar = (Progressbar) holder.getVIEw(R.ID.progressbar);      TextVIEw text = (TextVIEw) holder.getVIEw(R.ID.text);      if (mState == STATE_LOAD_COMPLETE) {        progressbar.setVisibility(VIEw.GONE);        text.setText(mContext.getString(R.string.没有数据了));      } else {        progressbar.setVisibility(VIEw.VISIBLE);        text.setText(mContext.getString(R.string.正在加载中));      }    }    if (holder.getItemVIEwType() == BaseRecyPRAdapter.VIEW_ITEM) {      onBindData(holder,position);    }  }  @OverrIDe  public int getItemCount() {    if (isShowFootVIEW) {      return mDataList == null ? 0 : mDataList.size() + 1;    } else {      return mDataList == null ? 0 : mDataList.size();    }  }  //根据不同的数据返回不同的vIEwType  @OverrIDe  public int getItemVIEwType(int position) {    if (mDataList == null)      return VIEW_ITEM;    if (position == mDataList.size()) {      return VIEW_PROG;    } else {      return VIEW_ITEM;    }  }  /**   * 根据type 返回不同的布局   *   * @param type   * @return   */  public abstract int getLayoutID(int type);  public abstract voID onBindData(BaseVIEwHolder holder,int position);  public static class BaseVIEwHolder extends RecyclerVIEw.VIEwHolder {    private Map<Integer,VIEw> mVIEwMap;    public VIEw vIEw;    public BaseVIEwHolder(VIEw itemVIEw) {      super(itemVIEw);      vIEw = itemVIEw;      mVIEwMap = new HashMap<>();    }    /**     * 获取设置的vIEw     *     * @param ID     * @return     */    public VIEw getVIEw(int ID) {      VIEw vIEw = mVIEwMap.get(ID);      if (vIEw == null) {        vIEw = itemVIEw.findVIEwByID(ID);        mVIEwMap.put(ID,vIEw);      }      return vIEw;    }  }  public voID stopLoadMore() {    mState = STATE_norMAL;    notifyDataSetChanged();  }  public voID loadComplete() {    mState = STATE_LOAD_COMPLETE;    notifyItemChanged(getItemCount()-1);  }  public voID setData(List<T> datas) {    mDataList = datas;  }  private LoadMoreDataListener mloadMoreDataListener;  public voID setLoadMoreDataListener(LoadMoreDataListener mloadMoreDataListener) {    this.mloadMoreDataListener = mloadMoreDataListener;  }  public interface LoadMoreDataListener {    voID loadMoreData();  }}

在activity中使用:

public class MainActivity extends AppCompatActivity {  private static final String TAG = "RecyActivity";  private RecyclerVIEw mRecyclerVIEw;  private List<String> List = new ArrayList<>();  private MyRecyVIEwAdapter mAdapter;  private Handler handler = new Handler();  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    init();  }  private voID init() {    initVIEw();    initData();    initListener();  }  private voID initVIEw() {    mRecyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.recyclerVIEw);    //创建一个linearlayoutmanager对象    linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(        this,linearlayoutmanager.VERTICAL,false);    mRecyclerVIEw.setLayoutManager(linearlayoutmanager);    //创建adapter对象    mAdapter = new MyRecyVIEwAdapter(this,mRecyclerVIEw);    mRecyclerVIEw.setAdapter(mAdapter);    mAdapter.setData(List);//设置数据  }  //初始化数据  private voID initData() {    for (int i = 0; i < 15; i++) {      List.add("DATA---------->" + i);    }  }  //初始化监听  private voID initListener() {    //加载更多回调监听    mAdapter.setLoadMoreDataListener(new BaseRecyPRAdapter.LoadMoreDataListener() {      @OverrIDe      public voID loadMoreData() {        //加入null值此时adapter会判断item的type        Log.e(TAG,"loadMoreData--->");        handler.postDelayed(new Runnable() {          @OverrIDe          public voID run() {            if (List.size() < 30) {              for (int i = 0; i < 5; i++) {                List.add("LOAD MORE DATA---------->" + i);              }              mAdapter.stopLoadMore();            } else {              mAdapter.loadComplete();            }          }        },500);      }    });  }  class MyRecyVIEwAdapter extends BaseRecyPRAdapter<String> {    public MyRecyVIEwAdapter(Context context,RecyclerVIEw recyclerVIEw) {      super(context,recyclerVIEw);    }    @OverrIDe    public int getLayoutID(int type) {      return R.layout.item_vIEw;    }    @OverrIDe    public voID onBindData(BaseVIEwHolder holder,int position) {      Log.e(TAG,"onBindData--->" + position);      if (mDataList == null || mDataList.isEmpty())        return;      TextVIEw tv = (TextVIEw) holder.getVIEw(R.ID.tv_name);      if (tv != null) {        tv.setText("DATA--------->" + position);      }    }  }}

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

总结

以上是内存溢出为你收集整理的RecyclerView上拉加载封装代码全部内容,希望文章能够帮你解决RecyclerView上拉加载封装代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144728.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存