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

相关推荐
plainGeekDev10 分钟前
HttpURLConnection → OkHttp + Kotlin
android·java·kotlin
QING61816 分钟前
Kotlin 协程新手指南 —— 协程基础与挂起函数
android·kotlin·android jetpack
2601_9617666423 分钟前
【分享】分身空间 2.3.7[特殊字符]生活工作互不打扰
android·生活
百度搜知知学社25 分钟前
抖音双模块架构:兼容全安卓版本并支持登录
android·架构·安卓·登录·兼容性·抖音
文阿花35 分钟前
Echarts实现柱状3D扇形图
android·3d·echarts
故渊at1 小时前
第六板块:Android 安全与权限体系 | 第十九篇:SELinux 强制访问控制与沙箱机制
android·安全·访问控制·selinux·权限体系·沙箱机制
千里马学框架1 小时前
重学Perfetto浏览器在线抓取trace及高频sql分享
android·sql·智能手机·架构·aaos·perfetto·车机
baozj1 小时前
把徒步轨迹做成 3D 地形模型:开源工具「印迹 TrailPrint 3D」
前端·vue.js·github
plainGeekDev1 小时前
批量写入 → Room 事务
android·java·kotlin
逛逛GitHub1 小时前
这 2 个免费的开源 Skill 太给劲儿,直接替代 Playwright。
github