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

相关推荐
sun0077003 小时前
android ndk编译valgrind
android
AI视觉网奇4 小时前
android studio 断点无效
android·ide·android studio
jiaxi的天空4 小时前
android studio gradle 访问不了
android·ide·android studio
No Silver Bullet5 小时前
android组包时会把从maven私服获取的包下载到本地吗
android
catchadmin5 小时前
PHP serialize 序列化完全指南
android·开发语言·php
Rhys..6 小时前
.gitignore文件的作用及用法
python·github
tangweiguo030519877 小时前
Kable使用指南:Android BLE开发的现代化解决方案
android·kotlin
CoderJia程序员甲8 小时前
GitHub 热榜项目 - 日榜(2025-09-12)
ai·github·开源项目·github热榜
蝎子莱莱爱打怪9 小时前
🚀🚀🚀嗨,一起来开发 开源IM系统呀!
前端·后端·github
00后程序员张9 小时前
iOS App 混淆与资源保护:iOS配置文件加密、ipa文件安全、代码与多媒体资源防护全流程指南
android·安全·ios·小程序·uni-app·cocoa·iphone