Android组件ListView

本文介绍如何使用 Android 中的 ListView 控件创建简单的和复杂的列表视图。包括使用数组适配器显示简单列表,以及利用哈希映射和简单适配器显示包含图片、产品名和类别的复杂列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、简单ListView控件
通过数组适配器把数组集合中每个元素信息用android.R.layout.simple_list_item_1方式放入ListView

点每一个item时,反应通过对listView对象设置setOnItemClickListener方法实现。
UI2Activity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_listview);
        ListView listView = (ListView)findViewById(R.id.listview_lv);
        final ArrayList arrayList = new ArrayList();
        arrayList.add("item1");
        arrayList.add("item2");
        arrayList.add("item3");
        ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(UI2Activity.this, ""+arrayList.get(position), Toast.LENGTH_SHORT).show();
            }
        });
}

界面布局layout_listview.xml

<?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">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listview_lv">
    </ListView>

</LinearLayout>

二、复杂ListView控件
用哈希网(键值对)的方式把信息建立每一个元素,再放入线性表中,通过简单适配器把线性表集合中每个元素信息用自定义的界面xml R.layout.listitemlayout,并对应指明键与自定义界面的控件名称放入ListView
UI2Activity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_listview);
        ListView listView = (ListView)findViewById(R.id.listview_lv);
        ArrayList arraylist = new ArrayList();
        HashMap hashMap;
        for (int i =0; i<20;i++) {
            hashMap = new HashMap();
            hashMap.put("product", "product1");
            hashMap.put("category", "category1");
            hashMap.put("img", android.R.drawable.ic_delete);
            arraylist.add(hashMap);

            hashMap = new HashMap();
            hashMap.put("product", "product2");
            hashMap.put("category", "category2");
            hashMap.put("img", android.R.drawable.ic_dialog_alert);
            arraylist.add(hashMap);

            hashMap = new HashMap();
            hashMap.put("product", "product3");
            hashMap.put("category", "category3");
            hashMap.put("img", android.R.drawable.ic_btn_speak_now);
            arraylist.add(hashMap);
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(this,arraylist,R.layout.listitemlayout,new String[]{"product","category","img"},new int[]{R.id.item_product,R.id.item_category,R.id.item_img});
        listView.setAdapter(simpleAdapter);
    }

每一个Item布局

<?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="horizontal">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item_img"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/item_product"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/item_category"/>
    </LinearLayout>
</LinearLayout>

界面布局layout_listview.xml

<?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">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listview_lv">
    </ListView>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值