public class Myadapter extends RecyclerView.Adapter{ private Context context; private List<bean1> list; public Myadapter(Context context, List<bean1> list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = null; RecyclerView.ViewHolder holder = null; switch (viewType){ case 0: view = View.inflate(context,R.layout.item1,null); holder = new ViewHloder1(view); break; case 1: view = View.inflate(context,R.layout.item2,null); holder = new ViewHloder2(view); break; } return holder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { switch (getItemViewType(position)){ case 0: ViewHloder1 viewHloder1 = (ViewHloder1) holder; String images = list.get(position).images;String[] split = images.split("\\|"); Glide.with(context).load(split[0]).into(viewHloder1.te_img1); viewHloder1.te_title1.setText(list.get(position).title); viewHloder1.te_jia1.setText(list.get(position).bargainPrice+""); System.out.println("++++++++++"+list.get(position).title); break; case 1: ViewHloder2 viewHloder2 = (ViewHloder2) holder; String images1 = list.get(position).images; String[] split1 = images1.split("\\|"); Glide.with(context).load(split1[0]).into(viewHloder2.te_img2); viewHloder2.te_title2.setText(list.get(position).title); viewHloder2.te_jia2.setText(list.get(position).bargainPrice+""); Glide.with(context).load(split1[0]).into(viewHloder2.te_img3); viewHloder2.te_title3.setText(list.get(position).title); viewHloder2.te_jia3.setText(list.get(position).bargainPrice+""); break; } } @Override public int getItemViewType(int position) { if(position%2==0) { return 0; }else { return 1; } } @Override public int getItemCount() { return list.size(); } public class ViewHloder1 extends RecyclerView.ViewHolder{ private final TextView te_title1; private final ImageView te_img1; private final TextView te_jia1; public ViewHloder1(View itemView) { super(itemView); te_title1 = itemView.findViewById(R.id.te_title1); te_img1 = itemView.findViewById(R.id.te_img1); te_jia1 = itemView.findViewById(R.id.te_jia1); } } public class ViewHloder2 extends RecyclerView.ViewHolder{ private final TextView te_title2; private final ImageView te_img2; private final TextView te_jia2; private final TextView te_title3; private final ImageView te_img3; private final TextView te_jia3; public ViewHloder2(View itemView) { super(itemView); te_title2 = itemView.findViewById(R.id.te_title2); te_img2 = itemView.findViewById(R.id.te_img2); te_jia2 = itemView.findViewById(R.id.te_jia2); te_title3 = itemView.findViewById(R.id.te_title3); te_img3 = itemView.findViewById(R.id.te_img3); te_jia3 = itemView.findViewById(R.id.te_jia3); } } }
11-07
11-07