recycleview 上拉加载下拉更新的适配器

public class DataAdapter extends RecyclerView.Adapter {
    Context context;
    List list;
    ItemClickListem itemClickListem;
    FooterViewHolder footerViewHolder;
    public MORE_STATUS status = LOADED;

    public DataAdapter(Context context, List list){
        this.context =context;
        this.list=list;

    }

    public void setItemClickListem(ItemClickListem itemClickListem) {
        this.itemClickListem = itemClickListem;
    }
    public void setStatus(MORE_STATUS status){

        this.status=status;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
       if (i==1){
           View view= LayoutInflater.from(context).inflate(R.layout.item_progress,viewGroup,false);
            footerViewHolder=new FooterViewHolder(view);
           return footerViewHolder;
       }else {
           View view= LayoutInflater.from(context).inflate(R.layout.rv_item,viewGroup,false);
           DataViewHolder dataViewHolder=new DataViewHolder(view);
           return dataViewHolder;
       }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (i!=list.size()){
            ((DataViewHolder)viewHolder).tv1.setText(list.get(i).getClsId());
            ((DataViewHolder)viewHolder).tv2.setText(list.get(i).getClsName());
            ((DataViewHolder)viewHolder).rv_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    /*Intent intent=new Intent(context, MainActivity2.class);
                    context.startActivity(intent);*/
                    itemClickListem.OnCli();
                }
            });
        }else {
            footerViewHolder.setData(status);
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position==list.size()){
            return 1;
        }else {
            return 0;
        }

    }

    @Override
    public int getItemCount() {
        return list.size()+1;
    }

    class DataViewHolder extends RecyclerView.ViewHolder {
        public TextView tv1,tv2;
        public LinearLayout rv_item;
        public DataViewHolder(@NonNull View itemView) {
            super(itemView);
            tv1=itemView.findViewById(R.id.weather_city);
            tv2=itemView.findViewById(R.id.weather_low);
            rv_item=itemView.findViewById(R.id.rv_item);
        }
    }
    public  class FooterViewHolder extends RecyclerView.ViewHolder{
        public  ProgressBar pb;
        public  TextView tv;
        public FooterViewHolder(@NonNull View itemView) {
            super(itemView);
            pb=itemView.findViewById(R.id.progressbar);
            tv=itemView.findViewById(R.id.zzjz);

        }
        public void setData(MORE_STATUS status){
            switch (status){
                case LOADING:
                    pb.setVisibility(View.VISIBLE);
                    tv.setVisibility(View.VISIBLE);
                    tv.setText("正在加载");
                    break;
                case LOADED:
                    pb.setVisibility(View.GONE);
                    tv.setVisibility(View.GONE);
                    break;
                case LOAD_COMPLETED:
                    pb.setVisibility(View.GONE);
                    tv.setVisibility(View.VISIBLE);
                    tv.setText("已经到底了");
                    break;

            }
        }
    }

    public interface ItemClickListem{
        public void OnCli();
    }

    public enum MORE_STATUS{
        LOADING,
        LOADED,
        LOAD_COMPLETED,
        REFLASHING,
        REFLASHED;
    }


}

你可能感兴趣的:(android)