banner/recyclerlayout在同adapter中使用

/*
*TODO  Adapter
*/
public class MyAdapter extends RecyclerView.Adapter {
    private ArrayList list;
    private ArrayList imageer;
    private Context context;

    public MyAdapter(ArrayList list, ArrayList imageer,
                     Context context) {
        this.list = list;
        this.imageer = imageer;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if (viewType == 0) {
            return new ViewHolder1(LayoutInflater.from(context).inflate(R.layout.item_my_bn,
                    parent, false));
        } else {
            return new ViewHolder2(LayoutInflater.from(context).inflate(R.layout.item_my_list,
                    parent, false));

        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int i = getItemViewType(position);
        if (i == 0) {
            ViewHolder1 holder1 = (ViewHolder1) holder;
            holder1.bn.setImages(imageer)
                    .setDelayTime(2000)
                    .setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object path, ImageView
                                imageView) {
                            String p = (String) path;
                            Glide.with(context).load(p).into(imageView);
                        }
                    }).start();
        } else {
            if (imageer.size()>0){
            position = position - 1;}
        ViewHolder2 holder2= (ViewHolder2) holder;
        holder2.tv.setText(list.get(position).getName());
        Glide.with(context).load(list.get(position).getIcon()).into(holder2.iv);
        }
    }

    @Override
    public int getItemCount() {
        if (imageer.size() > 0) {
            return list.size() + 1;
        } else {
            return list.size();
        }
    }

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

    public class ViewHolder1 extends RecyclerView.ViewHolder {
        @BindView(R.id.bn)
        Banner bn;

        public ViewHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ViewHolder2 extends RecyclerView.ViewHolder {
        @BindView(R.id.iv)
        ImageView iv;
        @BindView(R.id.tv)
        TextView tv;

        public ViewHolder2(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}

你可能感兴趣的:(banner/recyclerlayout在同adapter中使用)