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

相关推荐
阿巴斯甜1 小时前
ContentObserver的使用:
android
casual_clover1 小时前
【Android 控件】使用 Navigation 实现底部导航功能时左右滑动退出页面功能
android·底部导航滑动退出
urkay-1 小时前
Kotlin Flow分类
android·开发语言·kotlin
阿pin2 小时前
Android随笔-MVC/MVP/MVVM/MVI
android·mvc·mvvm·mvp·mvi
阿pin2 小时前
Android随笔-View绘制流程
android·开发语言·view
慕容引刀2 小时前
告别Commit信息纠结:使用Git AI Commit插件实现规范化提交
人工智能·git·github·visual studio code·visual studio
2501_916008892 小时前
iOS 证书管理最佳实践 从创建到续期的完整指南
android·ios·小程序·https·uni-app·iphone·webview
Digitally3 小时前
9 款优质无线传输软件,实现安卓设备与电脑互传数据
android·电脑
GoGeekBaird11 小时前
我开源了 BeeWeave,给 AI Agent 搭一个越用越懂你的知识创作台
后端·github
私人珍藏库11 小时前
[Android] PeakFinder AR v4.8.89 (山峰全景识别+增强现实山峰查看器)
android·人工智能·智能手机·ar·工具·软件