Android ListView 实例讲解清晰易懂

2022-07-21,,,,

一、前言

在某些场景下,单一文字的listview item已不适合当前需求,因此需要我们自定义item布局来满足需求。下面我们来实现一个带图标和文字的item

二、代码展示

1.定义包含listview的布局文件activity_main.xmlactivityoncreate()时加载。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffe4c4"
    tools:context=".mainactivity">

    <linearlayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margintop="10dp"
        android:layout_marginleft="10dp"
        android:layout_marginright="10dp"
        android:layout_marginbottom="10dp"
        android:background="#e4dddd">

        <listview
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </linearlayout>

</androidx.constraintlayout.widget.constraintlayout>

2.定义item布局文件listview_item.xml,创建simpleadapter对象时使用。

<?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"
    android:background="#f0fff0">

    <imageview
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"/>

    <textview
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="7"
        android:textcolor="#ff6e40"
        android:textsize="24sp"
        android:textstyle="bold" />



</linearlayout>

3.完善mainactivity.java代码。

package com.example.listviewdemo2;

import androidx.appcompat.app.appcompatactivity;

import android.os.bundle;
import android.widget.listview;
import android.widget.simpleadapter;

import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;

public class mainactivity extends appcompatactivity {

    private listview mlistview = null;
    private list<map<string, object>> mlistitems = null;
    private map<string, object> mmap = null;

    private simpleadapter madapter = null;

    /* 图片id数组 */
    private int[] mimageid = new int[] {r.drawable.num_0, r.drawable.num_1, r.drawable.num_2, r.drawable.num_3, r.drawable.num_4,
                                        r.drawable.num_5, r.drawable.num_6, r.drawable.num_7, r.drawable.num_8, r.drawable.num_9, };
    /* 文字列表数组 */
    private string[] mtitle = new string[] {"数字 0", "数字 1", "数字 2", "数字 3", "数字 4", "数字 5", "数字 6", "数字 7", "数字 8", "数字 9", };

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);

        init();
    }

    private void init() {
        mlistview = findviewbyid(r.id.listview);

        mlistitems = new arraylist<>();
        for (int i = 0; i < mimageid.length; i++) {
            mmap = new hashmap<>();
            mmap.put("image", mimageid[i]);
            mmap.put("title", mtitle[i]);
            mlistitems.add(mmap);
        }

        madapter = new simpleadapter(this, mlistitems, r.layout.listview_item, new string[]{"title", "image"}, new int[]{r.id.textview, r.id.imageview});
        mlistview.setadapter(madapter);
    }
}

三、运行效果

运行效果如下图:

到此这篇关于android listview 实例讲解清晰易懂的文章就介绍到这了,更多相关android listview内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《Android ListView 实例讲解清晰易懂.doc》

下载本文的Word格式文档,以方便收藏与打印。