显示图片名称,并设置点击事件
fruit.java还是不变的
public class Fruit {
private String name;
private int imgId;
public Fruit(String name,int imgId){
this.name=name;
this.imgId=imgId;
}
public String getName(){
return name;
}
public int getImgId(){
return imgId;
}
}
接下来写适配器
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{
//必须重写它的三个方法
private List<Fruit> mFruitList;
static class ViewHolder extends RecyclerView.ViewHolder{
ImageView fruitImage;
TextView fruitName;
View fruitView;
public ViewHolder(View view){
super(view);
fruitView=view;
fruitImage=view.findViewById(R.id.fruit_img);
fruitName=view.findViewById(R.id.fruit_name);
}
}
public FruitAdapter(List<Fruit> fruitList){
mFruitList=fruitList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
final ViewHolder holder=new ViewHolder(view);
holder.fruitView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
int position=holder.getAdapterPosition();
Fruit fruit=mFruitList.get(position);
Toast.makeText(v.getContext(),"you clicked view"+fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
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 clicked image"+fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
return holder;//将加载的实例返回
}
@Override
public void onBindViewHolder(FruitAdapter.ViewHolder holder, int position) {
//对recycleview子项的数据进行赋值
Fruit fruit=mFruitList.get(position);
holder.fruitImage.setImageResource(fruit.getImgId());;
holder.fruitName.setText(fruit.getName());
}
@Override
public int getItemCount() {
return mFruitList.size();
//返回数据源的长度
}
}
然后将布局加载进去
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fruit_img"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fruit_name"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:layout_marginLeft="10dp"/> <!--垂直方向居中显示-->
</LinearLayout>
主页面
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
RecyclerView recyclerView= (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter=new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
private void initFruits() {
for (int i=0;i<5;i++){
Fruit apple=new Fruit("Apple",R.drawable.apple_pic);
fruitList.add(apple);
Fruit banana=new Fruit("Banana",R.drawable.banana_pic);
fruitList.add(banana);
}
}
}
主页面布局
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view">
</android.support.v7.widget.RecyclerView>