public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{ public static final int ONE_ITEM = 1; public static final int TWO_ITEM = 2; public static final int Three_ITEM = 3; private List<MyData.ResultBean.DataBean> mList; private Context mContext; public MyAdapter(List<MyData.ResultBean.DataBean> mList, Context mContext) { this.mList = mList; this.mContext = mContext; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); RecyclerView.ViewHolder holder; View inflate; if (ONE_ITEM == viewType) { inflate = inflater.inflate(R.layout.item_one, parent, false); holder = new ViewHolderOne(inflate); } else if (TWO_ITEM == viewType) { inflate = inflater.inflate(R.layout.item_two, parent, false); holder = new ViewHolderTwo(inflate); } else { inflate = inflater.inflate(R.layout.item_three, parent, false); holder = new ViewHolderThree(inflate); } inflate.setOnClickListener(this); return holder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { MyData.ResultBean.DataBean dataBean = mList.get(position); //参数1是否是参数2 的一个实例对象 if (holder instanceof ViewHolderOne) { Picasso.with(mContext).load(dataBean.getUrl()).into(((ViewHolderOne) holder).mImage); Picasso.with(mContext).load(dataBean.getUrl()).into(((ViewHolderOne) holder).mImage); ((ViewHolderOne) holder).mText.setText(dataBean.getContent()); ((ViewHolderOne) holder).itemView.setTag(position); } else if (holder instanceof ViewHolderTwo) { Picasso.with(mContext).load(dataBean.getUrl()).into(((ViewHolderTwo) holder).mImage); ((ViewHolderTwo) holder).mText.setText(dataBean.getContent()); ((ViewHolderTwo) holder).itemView.setTag(position); } else { Picasso.with(mContext).load(dataBean.getUrl1()).into(((ViewHolderThree) holder).mImageOne); Picasso.with(mContext).load(dataBean.getUrl2()).into(((ViewHolderThree) holder).mImageTwo); ((ViewHolderThree) holder).mTextOne.setText(dataBean.getContent()); ((ViewHolderThree) holder).mTextTwo.setText(dataBean.getContent()); ((ViewHolderThree) holder).itemView.setTag(position); } } @Override public int getItemViewType(int position) { // MyData.ResultBean.DataBean dataBean = mList.get(position); // dataBean.getType(); if (position <= 3) { return ONE_ITEM; } else if (position == 4) { return TWO_ITEM; } else { return Three_ITEM; } } @Override public int getItemCount() { return mList.isEmpty() ? 0 : mList.size(); } class ViewHolderOne extends RecyclerView.ViewHolder { private ImageView mImage; private TextView mText; public ViewHolderOne(View itemView) { super(itemView); mImage = (ImageView) itemView.findViewById(R.id.One_Item_Image); mText = (TextView) itemView.findViewById(R.id.One_Item_Text); } } class ViewHolderTwo extends RecyclerView.ViewHolder { private ImageView mImage; private TextView mText; public ViewHolderTwo(View itemView) { super(itemView); mImage = (ImageView) itemView.findViewById(R.id.Two_Item_Image); mText = (TextView) itemView.findViewById(R.id.Two_Item_Text); } } class ViewHolderThree extends RecyclerView.ViewHolder { private ImageView mImageOne, mImageTwo; private TextView mTextOne, mTextTwo; public ViewHolderThree(View itemView) { super(itemView); mImageOne = (ImageView) itemView.findViewById(R.id.Three_Item_Image_One); mImageTwo = (ImageView) itemView.findViewById(R.id.Three_Item_Image_Two); mTextOne = (TextView) itemView.findViewById(R.id.Three_Item_Text_One); mTextTwo = (TextView) itemView.findViewById(R.id.Three_Item_Text_Two); } } //自定义接口 public interface OnClickItem { void setClick(View v, int position); } private OnClickItem item; @Override public void onClick(View v) { if (item != null) { item.setClick(v, (int) v.getTag()); } } public void setOnClick(OnClickItem item) { this.item = item; } }