Android studio ListView应用设计

一、添加ListView控件:

复制代码
<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="1dp" />

二、类文件Country.java:

复制代码
package com.july.listviewapp;

public class Country {
    private String name;
    private int icon;


    public Country(String name,int icon){
        this.name = name;
        this.icon = icon;
    }

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }

    public int getIcon(){
        return  icon;
    }

    public void setIcon(int icon){
        this.icon = icon;
    }

}

三、子布局文件:

复制代码
<?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"
    android:orientation="vertical"
    android:padding="5dp">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="80dp"
        android:layout_height="64dp"
        android:layout_centerInParent="false"
        android:src="@drawable/china_icon" />

    <TextView
        android:id="@+id/TextView"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="china"
        android:textSize="18sp" />

</LinearLayout>

四、Adapter文件:

五、MainActivity.java文件:

复制代码
    context = MainActivity.this;
    listView = findViewById(R.id.listView);

    countryList = new ArrayList<Country>();
    countryList.add(new Country("china",R.drawable.china_icon));
    countryList.add(new Country("germany",R.drawable.germany_icon));
    countryList.add(new Country("finland",R.drawable.finland_icon));
    countryList.add(new Country("mexico",R.drawable.mexico_icon));
    countryList.add(new Country("india",R.drawable.india_icon));

    countryAdapter = new CountryAdapter((List<Country>) countryList,context);
    listView.setAdapter(countryAdapter);

    listView.setOnItemClickListener(MainActivity.this);
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    Toast.makeText(context,"您点击了第" + i + "项",Toast.LENGTH_SHORT).show();
}

六、运行效果:

七、工程源码:

AndroidstudioListView应用设计资源-CSDN文库

相关推荐
小弥儿15 分钟前
GitHub今日热榜 | 2026-09-08:微软 markitdown 冲进前三
学习·microsoft·开源·github
hunterandroid43 分钟前
Android 线上卡顿治理:从布局层级到主线程负载的全链路排查
android
蜡台1 小时前
超详细 GitHub Actions 自动发布部署教程
github·github page·github action
恋猫de小郭1 小时前
Dart Skills CLI 1.0 :AI 时代的 Dart 交付支持
android·前端·flutter
2501_916007471 小时前
使用Apple Dashboards显示和自定iOS应用性能指标与数据可视化指南
android·ios·小程序·https·uni-app·iphone·webview
11路没有终点2 小时前
CI/CD 集成:GitHub Actions
ci/cd·github
蜡台2 小时前
# 已解决|MySQL 8\.4\.11 密码正确仍报错1045 \(28000\) Access denied 终极修复方案
android·mysql·adb
JMchen1232 小时前
2026年六款主流AI编程工具深度实测:Cursor、Copilot、Claude Code等对比与选型思考
android·kotlin·copilot·ai编程·开发工具·cursor·claude code
右耳朵猫AI2 小时前
Github周刊2026W36 | Archify架构图生成、科研Agent技能库、VoiceStudio本地语音、MiniMind训练6400万参数
github
DeepAgent3 小时前
AI Agent 开发实战(13):GitHub 开源
开源·github·agent