須要在Adapter適配器中設置ide
使用getItemViewType()方法設置根據不一樣ID來判斷設置佈局佈局
public class Adapter extends RecyclerView.Adapter<Adapter.MyHolder> { private Context context; private List<Bean> list; public Adapter(Context context, List<Bean> 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); } } }