Fruit:
public class Fruit {
private String name;
private int imageId;
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
}
fruit_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:orientation="vertical">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="10dp" />
</LinearLayout>
FruitAdapter:
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List<Fruit> fruits;
public FruitAdapter(List<Fruit> fruits) {
this.fruits = fruits;
}
static class ViewHolder extends RecyclerView.ViewHolder {
View fruitView;
ImageView fruitImage;
TextView fruitName;
public ViewHolder(@NonNull View itemView) {
super(itemView);
fruitView = itemView;
fruitImage = itemView.findViewById(R.id.fruit_image);
fruitName = itemView.findViewById(R.id.fruit_name);
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
viewHolder.fruitView.setOnClickListener(v -> {
int position = viewHolder.getAdapterPosition();
Fruit fruit = fruits.get(position);
Toast.makeText(parent.getContext(), fruit.getName(), Toast.LENGTH_LONG).show();
});
viewHolder.fruitImage.setOnClickListener(v -> {
Toast.makeText(parent.getContext(), "你点击是的图片" + viewHolder.getAdapterPosition(), Toast.LENGTH_LONG).show();
});
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Fruit fruit = fruits.get(position);
holder.fruitImage.setImageResource(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
}
@Override
public int getItemCount() {
return fruits.size();
}
}
MainActivity:
public class MainActivity extends BaseActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "MainActivity onCreate");
setContentView(R.layout.activity_main);
this.initView();
}
/**
* 初始化View
*/
private void initView() {
// 初始化RecyclerView
RecyclerView recyclerView = findViewById(R.id.main_rv);
// 数据源
List<Fruit> fruits = new ArrayList<>();
for (int i = 0; i < 30; i++) {
fruits.add(new Fruit("水果" + i, R.drawable.ic_launcher_foreground));
}
// 创建布局管理器
// LinearLayoutManager layoutManager = new LinearLayoutManager(this);
// layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
// 设置布局管理器
recyclerView.setLayoutManager(layoutManager);
// 创建适配器
FruitAdapter adapter = new FruitAdapter(fruits);
// 设置适配器
recyclerView.setAdapter(adapter);
}
}