1 与ListView
的比较
-
与
ListView
不同,RecyclerView
并没有提供类似于setOnItemClickListener( )
这样的注册监听器方法,而是需要我们自己给子项具体的View
去注册点击事件 -
摒弃了
ListView
子项点击事件的监听器,所有的点击事件都有具体的View去注册
2 在自定义Adapter中设置点击事件
步骤:
- 在
public FruitAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
中设置点击事件 - 从
viewHolder
中获取子布局,并设置子布局的点击事件,这样可以在RecyclerView
的子布局中点击子控件,并响应
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
/**
* 1. FruitAdapter 继承与RecyclerAdapter,
* */
private List<Fruit> fruits;
// 1. 构造方法传入数据源
public FruitAdapter(List<Fruit> fruits) {
this.fruits = fruits;
}
@NonNull
@Override
// 创建ViewHolder实例, 将加载的布局放到构造函数中,并将View返回
public FruitAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// 将填充的view放到viewHolder中
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_layout_staggered, parent, false);
ViewHolder holder = new ViewHolder(view);
// 1. 在创建的时候进行点击事件
// 从viewHolder中获取子空间, 添加点击事件
// 1.1 子布局的点击事件
holder.getFruitView().setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view1) {
// 获取点击的位置,定位数组并输出信息
int adapterPosition = holder.getAdapterPosition();
Fruit fruit = fruits.get(adapterPosition);
Toast.makeText(view1.getContext(), "点击的布局,其中内容为: " + fruit.getName(), Toast.LENGTH_SHORT).show();
}
});
// 1.2 获取图片的点击事件
holder.getFruitImage().setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view2) {
int adapterPosition = holder.getAdapterPosition();
Fruit fruit = fruits.get(adapterPosition);
Toast.makeText(view2.getContext(), "点击图片的水果名称为: " + fruit.getName(), Toast.LENGTH_SHORT).show();
}
});
// 1.3 为文本框指定点击事件
holder.getFruitName().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view3) {
String name = ((TextView)view3.findViewById(R.id.fruit_name)).getText().toString();
Toast.makeText(view3.getContext(), name, Toast.LENGTH_SHORT).show();
}
});
return holder;
}
@Override
// 2. 每个子项滚动到屏幕内执行,通过 positon 得到当前项的实例,然后显示图片和文字,填充布局
public void onBindViewHolder(@NonNull FruitAdapter.ViewHolder holder, int position) {
Fruit fruit = fruits.get(position);
holder.getFruitImage().setImageResource(fruit.getImageID());
holder.getFruitName().setText(fruit.getName());
holder.getFruitPrice().setText("$" + (double)fruit.getPrice()/100 + "/Kg");
}
@Override
public int getItemCount() {
return fruits.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
// 添加ViewHolder中的控件
private View fruitView;
private ImageView fruitImage;
private TextView fruitName;
private TextView fruitPrice;
public ViewHolder(@NonNull View itemView) {
super(itemView);
this.fruitView = itemView;
this.fruitImage = itemView.findViewById(R.id.fruit_image);
this.fruitName = itemView.findViewById(R.id.fruit_name);
this.fruitPrice = itemView.findViewById(R.id.fruit_price);
}
public void setFruitView(View fruitView) {
this.fruitView = fruitView;
}
public void setFruitImage(ImageView fruitImage) {
this.fruitImage = fruitImage;
}
public void setFruitName(TextView fruitName) {
this.fruitName = fruitName;
}
public void setFruitPrice(TextView fruitPrice) {
this.fruitPrice = fruitPrice;
}
public ImageView getFruitImage() {
return fruitImage;
}
public TextView getFruitName() {
return fruitName;
}
public TextView getFruitPrice() {
return fruitPrice;
}
public View getFruitView() {
return fruitView;
}
}
}