滚动菜单+图片ListView

目录

Fruit.java

FruitAdapter

MainActivity

activity_main.xml

fruit.xml


整体结构

Fruit.java

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 int getImageId() {
        return imageId;
    }
}

FruitAdapter

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class FruitAdapter extends ArrayAdapter<Fruit> {
    private int resourceId;

    public FruitAdapter(Context context, int resource, List<Fruit> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Fruit fruit = getItem(position); // 获取当前 fruit 实例

        // 加载布局文件
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
        }

        // 获取布局文件中的 ImageView 和 TextView 控件
        ImageView fruitImage = convertView.findViewById(R.id.fruit_image);
        TextView fruitName = convertView.findViewById(R.id.fruit_name);

        // 设置 ImageView 和 TextView 的值
        fruitImage.setImageResource(fruit.getImageId());
        fruitName.setText(fruit.getName());

        return convertView;
    }
}

MainActivity

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

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);

        // 隐藏系统自带标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        // 初始化水果列表
        initFruits();

        // 创建自定义适配器
        FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit, fruitList);

        // 将适配器设置给 ListView
        ListView listView = findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        //添加一个点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Fruit fruit = fruitList.get(position);
                Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initFruits() {
        // 添加水果数据到列表
        for (int i = 0; i < 2; i++) {
            Fruit m0 = new Fruit("苹果", R.drawable.m0);
            fruitList.add(m0);
            Fruit m1 = new Fruit("苹果", R.drawable.m1);
            fruitList.add(m1);
            Fruit m2 = new Fruit("苹果", R.drawable.m2);
            fruitList.add(m2);
            Fruit m3 = new Fruit("苹果", R.drawable.m3);
            fruitList.add(m3);
            Fruit m4 = new Fruit("苹果", R.drawable.m4);
            fruitList.add(m4);
            Fruit m5 = new Fruit("苹果", R.drawable.m5);
            fruitList.add(m5);
            Fruit m6 = new Fruit("苹果", R.drawable.m6);
            fruitList.add(m6);
            Fruit m7 = new Fruit("苹果", R.drawable.m7);
            fruitList.add(m7);
            Fruit m8 = new Fruit("苹果", R.drawable.m8);
            fruitList.add(m8);
            Fruit m9 = new Fruit("苹果", R.drawable.m9);
            fruitList.add(m9);
        }
    }
}

activity_main.xml ⬅ 跳转上篇文章

fruit.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="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".Fruit">

    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_marginLeft="10dp"/>

</LinearLayout>

最终效果

PNG资源素材请跳转 iconfont-阿里巴巴矢量图标库

相关推荐
游客5201 分钟前
设计模式-创建型-工厂方法模式
开发语言·python·设计模式·工厂方法模式
m0_748234905 分钟前
Hmsc包开展群落数据联合物种分布模型分析通用流程(Pipelines)
开发语言·python
liangmou212112 分钟前
解释小部分分WPI函数(由贪吃蛇游戏拓展)
android·游戏·c#
WongKyunban14 分钟前
bash shell脚本while循环
开发语言·bash
想成为高手49917 分钟前
华为仓颉编程语言的函数与结构类型分析
开发语言·华为
lly20240633 分钟前
Ruby 数据库访问 - DBI 教程
开发语言
星就前端叭39 分钟前
【开源】一款基于SpringBoot的智慧小区物业管理系统
java·前端·spring boot·后端·开源
带刺的坐椅40 分钟前
RxSqlUtils(base R2dbc)
java·reactor·solon·r2dbc
m0_7482540942 分钟前
100天精通Python(爬虫篇)——第113天:爬虫基础模块之urllib详细教程大全
开发语言·爬虫·python
silence2501 小时前
深入了解 Reactor:响应式编程的利器
java·spring