Aitem XML 3张图片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/img" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> <ImageView android:id="@+id/img2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> <ImageView android:id="@+id/img3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:id="@+id/name" android:text="hh" android:textSize="20sp"/> </LinearLayout>
Bitem XML 1张图片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:id="@+id/name" android:text="hh" android:textSize="20sp"/> <ImageView android:id="@+id/img3" android:layout_marginRight="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> </LinearLayout>
适配器
public class AMyAdapter extends RecyclerView.Adapter { private List<MyBean.ResultBean.DataBean> data; private Context mc; public AMyAdapter(List<MyBean.ResultBean.DataBean> data, Context mc) { this.data = data; this.mc = mc; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == 1) { View inflate2 = LayoutInflater.from(mc).inflate(R.layout.item_b, null); return new Holder2(inflate2); } if (viewType == 3) { View inflate = LayoutInflater.from(mc).inflate(R.layout.item_a, null); return new Holder(inflate); } return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof Holder) { Picasso.with(mc).load(data.get(position).getThumbnail_pic_s()).into(((Holder) holder).img1); Picasso.with(mc).load(data.get(position).getThumbnail_pic_s02()).into(((Holder) holder).img2); Picasso.with(mc).load(data.get(position).getThumbnail_pic_s03()).into(((Holder) holder).img3); ((Holder) holder).name.setText(data.get(position).getTitle()); } if (holder instanceof Holder2) { Picasso.with(mc).load(data.get(position).getThumbnail_pic_s()).into(((Holder2) holder).img); ((Holder2) holder).name.setText(data.get(position).getTitle()); } } @Override public int getItemViewType(int position) { if (data.get(position).getThumbnail_pic_s03() != null) { return 3; } else { return 1; } } @Override public int getItemCount() { return data.size(); } public class Holder extends RecyclerView.ViewHolder { private ImageView img1, img2, img3; private TextView name; public Holder(View itemView) { super(itemView); name = itemView.findViewById(R.id.name); img1 = itemView.findViewById(R.id.img); img2 = itemView.findViewById(R.id.img2); img3 = itemView.findViewById(R.id.img3); } } public class Holder2 extends RecyclerView.ViewHolder { private ImageView img; private TextView name; public Holder2(View itemView) { super(itemView); img = itemView.findViewById(R.id.img3); name=itemView.findViewById(R.id.name); } } }