Fruit.Class FruitAdapter.Class activity_main.Xml和地址里的一样
http://blog.csdn.net/cccrunner/article/details/70502494
FruitAdapter.Class
package com.example.deligence.demo10; public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{ List<Fruit> mfruitList; static class ViewHolder extends RecyclerView.ViewHolder { View fruitView; //保存子项最外层布局的实例 ImageView fruitImage; TextView fruitName; public ViewHolder(View View) { super(View); fruitView = View; fruitImage = (ImageView) View.findViewById(R.id.fruit_image); fruitName = (TextView) View.findViewById(R.id.fruit_name); } } public FruitAdapter(List<Fruit> mfruitList) { this.mfruitList = mfruitList; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit,parent,false); final ViewHolder holder = new ViewHolder(view); //最外层布局注册了监听事件 holder.fruitView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getAdapterPosition(); //先从点击事件中获得用户点击的position Fruit fruit = mfruitList.get(position); //然后通过position拿到Fruit的实例 Toast.makeText(v.getContext(),"You click the View"+fruit.getFruitName(),Toast.LENGTH_SHORT).show(); //再使用toast弹出两种不同的内容以示区别 } }); //imageview注册了监听事件 holder.fruitImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getAdapterPosition(); Fruit fruit = mfruitList.get(position); Toast.makeText(v.getContext(),"You click the View"+fruit.getFruitName(),Toast.LENGTH_SHORT).show(); } }); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { Fruit fruit = mfruitList.get(position); holder.fruitImage.setImageResource(fruit.getFruitImageId()); holder.fruitName.setText(fruit.getFruitName()); } @Override public int getItemCount() { return mfruitList.size(); } }