RecycleView 上拉加载下拉刷新 分页加载

1.首先先看适配器


import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import java.util.List;


public class MerchantListRecyclerAdapter extends Adapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private Context context;
    private List data;

    public static final int PULL_TO_LOAD_MORE = 0;
    public static final int LOADING = 1;
    public static final int NO_MORE = 2;
    private int mFooterStatus=PULL_TO_LOAD_MORE;


    public MerchantListRecyclerAdapter(Context context, List data) {
        this.context = context;
        this.data = data;
    }

    /**
     * ListView数据发生变化时,调用此方法来更新ListView
     * @param list
     */
    public void updateListView(List list){
        this.data = list;
        notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return data.size() == 0 ? 0 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.merchants_list_item, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }

    protected void bindFooter(FootViewHolder holder) {
        switch (mFooterStatus){
            case PULL_TO_LOAD_MORE:
                holder.progressBar.setVisibility(View.VISIBLE);
                holder.tvLoad.setText( "上拉加载更多...");
                break;
            case LOADING:
                holder.progressBar.setVisibility(View.VISIBLE);
                holder.tvLoad.setText( "正在拼命加载...");
                break;
            case NO_MORE:
                holder.tvLoad.setText("没有更多了!");
                holder.progressBar.setVisibility(View.INVISIBLE);
                break;
        }
    }

    public void setFooterStatus(int status){
        mFooterStatus=status;
        notifyDataSetChanged();
    }

    public int getFooterStatus(){
        return mFooterStatus;
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            //为Item填充数局
            
            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemClick(holder.itemView, position);
                    }
                });
            }

        }else{
            bindFooter((FootViewHolder) holder);
        }
    }

    static class ItemViewHolder extends ViewHolder {

        public ItemViewHolder(View view) {
            super(view);
             //item布局初始化

} } static class FootViewHolder extends ViewHolder { TextView tvLoad ; ProgressBar progressBar ; public FootViewHolder(View view) { super(view) ; tvLoad = (TextView)view.findViewById(R.id. tv_load) ; progressBar = (ProgressBar) view.findViewById(R.id. progressBar) ; } }}


2.在Activity中的具体使用

@Override
public void setViews() {
    super.setViews();
    merchantsListSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
    mAdapter = new MerchantListRecyclerAdapter(this,mMerchantsData);

    //下拉刷新监听回调
    merchantsListSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
         //下拉刷新回调
 }
    });

    layoutManager = new LinearLayoutManager(this);
    merchantsListListView.setLayoutManager(layoutManager);
    merchantsListListView.setAdapter(mAdapter);
}

@Override
public void setValues() {
    super.setValues();

    //RecyclerView滑动监听
    merchantsListListView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_IDLE
                    && lastVisibleItemPosition + 1 == mAdapter.getItemCount()
                    && mAdapter.getFooterStatus()!=TripTeamListRecyclerAdapter.NO_MORE) {
                //上拉加载更多回调
            }
        }
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
        }
    });

    //添加点击事件
    mAdapter.setOnItemClickListener(new MerchantListRecyclerAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
        }
    });
}

3、当上拉加载更多时,若没有查询到数据  则代表没有数据

可设置,底层item显示成无数据

mAdapter.setFooterStatus(TripTeamListRecyclerAdapter.NO_MORE);

你可能感兴趣的:(Android,RecycleView,上拉加载,下拉刷新,分页加载)