Android开发 RecyclerView+SwipeRefreshlayout实现上拉刷新和分页加载

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

前言:

我们在项目开发过程中一般都有"分页加载"的需求,分页加载就是以列表的方式显示数据,在有网络的前提下,我们可以通过下拉刷新手动获取更多的数据;没有更多的数据的时候,会提醒我们没有更多数据。        

用到的知识点:

  1. Retrofit实现网络请求
  2. Recyclerview展示列表信息
  3. Swiperefreshlayout实现下拉刷新,上拉加载更多
  4. Picasso加载图片

实现代码

1.配置gradle依赖

//retrofit
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
//recyclerview
compile 'com.android.support:recyclerview-v7:25.0.1'
//picasso
compile 'com.squareup.picasso:picasso:2.3.2'

2.在layout布局文件中添加SwipyRefreshLayout



    

        
        

            

                

                
            
        
    

3.适配器的代码

​
public class OfferLoansRVAdapter extends RecyclerView.Adapter implements View.OnClickListener {
    public List LoansBeens;
    public Context context;

    public OfferLoansRVAdapter(Context context, List loansBeans) {
        super();
        this.context = context;
        this.LoansBeens = loansBeans;
    }

    //声明OnRecyclerViewListener对象
    private OfferLoansRVAdapter.OnRecyclerViewListener mOnRecyclerViewListener = null;

    @Override
    public void onClick(View v) {
        if (mOnRecyclerViewListener != null) {
            mOnRecyclerViewListener.onItemClick(v, (Offer_personal_loans.LoansBean) v.getTag());
        }
    }

    //定义OnReccyclerViewListener接口
    public interface OnRecyclerViewListener {
        void onItemClick(View view, Offer_personal_loans.LoansBean loansBean);
    }

    //设置监听器用来传递数据给上面定义的接口对象的方法
    public void setOnItemClickListener(OfferLoansRVAdapter.OnRecyclerViewListener listener) {
        this.mOnRecyclerViewListener = listener;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //R.layout.common_loan_item:item的布局文件
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.common_loan_item, parent, false);
        OfferLoansRVAdapter.ViewHolder vh = new OfferLoansRVAdapter.ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
       if (holder instanceof ViewHolder) {
        try {
            String logo_url = LoansBeens.get(position).getLogo_url();
            Picasso.with(Global.mContext).load(logo_url).into(holder.imgLoanLogo);
            ...

            holder.imgLoanCheck.setVisibility(View.VISIBLE);
            //将position保存在itemView的标记中,以便实现点击事件
            holder.itemView.setTag(LoansBeans.get(position));
        } catch (Exception e) {
           e.printStackTrace();
        }
      }
    }

    /*
     *这是重要的方法,在分页加载数据的时候用来添加List数据
     */
    public void addItem(List loansBeans) {
        try {
            for (Offer_personal_loans.LoansBean loansBean : loansBeans)
                LoansBeens.add(loansBean);
        } catch (ConcurrentModificationException e) {
            e.printStackTrace();
        }

    }

    @Override
    public int getItemCount() {
        return LoansBeens == null ? 0 : LoansBeens.size();
    }

    public Offer_personal_loans.LoansBean getItem(int position) {
        return LoansBeens.get(position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imgLoanLogo;
        ...

        public ViewHolder(View view) {
            super(view);
            imgLoanLogo = (ImageView) view.findViewById(R.id.img_loan_logo);
            ...
        }
    }
}



​

4.在Fragment实现的代码

public class OfferPersonalLoansFragment extends BaseFragment implements SwipyRefreshLayout.OnRefreshListener {

    ...

    @Override
    public int getLayoutRes() {
        return R.layout.offer_personal_loans;
    }

    @Override
    public void initView() {
       ...
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
         //网络请求数据      
        }
    }

    @Override
    public void onClick(View v, int id) {
      ...
    }

     /*
      *网络请求成功后调用的方法
      */
    @Override
    public void onHttpSuccess(int reqType, Message msg) {
        if (reqType == IHttpService.HTTP_GET_OFFER_PERSONAL_LOANS) {
            hideProgressDialog();
            offerPersonalLoans = (Offer_personal_loans) msg.obj;
            try {
                personalLoansBeen = offerPersonalLoans.getLoans();
                offerPersonalLoansList = new ArrayList<>();
                offerPersonalLoansList.add(personalLoansBeen);
            } catch (Exception e) {
                e.printStackTrace();
            }
            initLoanRecyclerView();
            tvBottomText.setVisibility(View.VISIBLE);
        }
    }

    private void initLoanRecyclerView() {
        if (offerPersonalLoansRVAdapter == null) {//判空
            offerPersonalLoansRVAdapter = new OfferLoansRVAdapter(getActivity(), personalLoansBeen);
            rvOfferPersonalLoans.setAdapter(offerPersonalLoansRVAdapter);
        } else {
            //之前已经有加载数据了,手动刷新/分页加载数据的时候,这里是添加List数据
            offerPersonalLoansRVAdapter.notifyDataSetChanged();
            offerPersonalLoansRVAdapter.addItem(personalLoansBeen);
           
        }

        refreshLayout.setRefreshing(false);//这一句要记得加上

        //统一处理点击事件
        offerPersonalLoansRVAdapter.setOnItemClickListener(new  OfferLoansRVAdapter.OnRecyclerViewListener() {
            @Override
            public void onItemClick(View view, Offer_personal_loans.LoansBean loansBean) {
              ...
            }
        });
    }

    /*
     *上拉刷新,请求接口
     */
    @Override
    public void onRefresh(int index) {
          //do something
    }
    
    /*
     *分页加载,请求接口
     */
    @Override
    public void onLoad(int index) {
         //do something
    }

     /*
      *网络请求失败后调用的方法
      */
    @Override
    public void onHttpError(int reqType, ArrayList error) {
       //do something
    }

    /*
      *网络timeout调用的方法
      */
    @Override
    public void onHttpFailure(int reqType, String error) {
        //do something
    }
    
    /*
     *服务器错误调用的方法
     */
    @Override
    public void onServerError(int reqType) {
       //do something
    }

}

5.总结:分页加载就是一页一页加载数据,当滑动到底部、没有更多数据加载的时候,我们可以手动调用接口,重新刷新RecyclerView。上拉功能就不能用 ,只有下拉刷新过后,才能重新上拉加载数据,这样的话不仅能够减轻服务器的负担,也能让用户节省流量,从而提高用户体验。

转载于:https://my.oschina.net/wupeilin/blog/1632828

你可能感兴趣的:(Android开发 RecyclerView+SwipeRefreshlayout实现上拉刷新和分页加载)