需要在Adapter适配器中设置
使用getItemViewType()方法设置根据不同ID来判断设置布局
public class Adapter extends RecyclerView.Adapter{ private Context context; private List list; public Adapter(Context context, List list) { this.context = context; this.list = list; } /** * recyclerView 加载多布局方法步骤 * 1. 建两个不同的布局 * 2. 思路:要提供一个不同的标准,标注 程序选择不同的布局 */ @Override public int getItemViewType(int position) { return list.get(position).getName().contains("2")==true?0:1; } /** * 实例化一个布局 * @param parent * @param viewType <----getItemviewType * @return */ @Override public Adapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { MyHolder myHolder = null; switch (viewType){ case 0: View inflate = LayoutInflater.from(context).inflate(R.layout.layout, null); myHolder = new MyHolder(inflate); break; case 1: View inflate1 = LayoutInflater.from(context).inflate(R.layout.layout1, null); myHolder = new MyHolder(inflate1); break; } return myHolder; } @Override public void onBindViewHolder(Adapter.MyHolder holder, int position) { holder.img.setImageResource(list.get(position).getImg()); holder.name.setText(list.get(position).getName()); holder.text.setText(list.get(position).getText()); } @Override public int getItemCount() { return list.size(); } /** * 各种布局,进行重用的view */ public class MyHolder extends RecyclerView.ViewHolder { private final ImageView img; private final TextView name; private final TextView text; public MyHolder(View itemView) { super(itemView); img = itemView.findViewById(R.id.img); name = itemView.findViewById(R.id.name); text = itemView.findViewById(R.id.text); } }}