主函数:
public class ThirdActivity extends Activity{ private RecyclerView recyclerView; private ThirdAdapter thirdAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third); recyclerView = (RecyclerView) findViewById(R.id.third_rc); thirdAdapter = new ThirdAdapter(this); recyclerView.setLayoutManager(new GridLayoutManager(this,4)); recyclerView.setAdapter(thirdAdapter); }
}
适配器Adapter:
public class ThirdAdapter extends RecyclerView.Adapter<ThirdAdapter.ThirdViewHolder>{ private List<String> data; private Context context; private List<Integer> integers; public ThirdAdapter(Context context) { this.context = context; inisData(); } private void inisData() { data = new ArrayList<>(); integers = new ArrayList<>(); for (int i = 0; i < 100; i++) { data.add("第" + i + "条"); integers.add(R.mipmap.ic_launcher); } } @Override public ThirdViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item_third,null); ThirdViewHolder thirdViewHolder = new ThirdViewHolder(view); return thirdViewHolder; } @Override public void onBindViewHolder(ThirdViewHolder holder, int position) { holder.textView.setText(data.get(position)); holder.imageView.setImageResource(integers.get(position)); } @Override public int getItemCount() { return data.size(); } class ThirdViewHolder extends RecyclerView.ViewHolder{ TextView textView;ImageView imageView; public ThirdViewHolder(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.item_third_tv); imageView = (ImageView) itemView.findViewById(R.id.item_third_iv); } } }