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文库

相关推荐
m0_748233641 分钟前
RabbitMQ 进阶
android·前端·后端
Mr.pyZhang4 小时前
Android构建系统 - 04 编译产物
android·linux
go54631584657 小时前
本地部署 GitHub 上的 Python 人脸识别项目
开发语言·python·github
守城小轩10 小时前
Brave 132 编译指南 Android 篇 - 编译准备:系统要求与推荐工具 (三)
android·chrome·chrome devtools·指纹浏览器·浏览器开发·brave
m0_7482384210 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
程序员江同学11 小时前
Kotlin 技术月报 | 2025 年 2 月
android·kotlin
甜于酸12 小时前
MuMu模拟器Pro for Mac 安卓手机平板模拟器
android·macos·智能手机
顾林海15 小时前
深入解析 Android Activity 生命周期
android
XuanRanDev17 小时前
【构建工具】Gradle Kotlin DSL中的大小写陷阱:BuildConfigField
android·开发语言·kotlin
Nathan2024061619 小时前
Room - 基本使用及使用问题
android·面试·android jetpack