Android学习|布局列表—— ListView

这篇博客介绍了如何在Android中实现ListView,包括自定义列表项Item,模拟数据,获取ListView并使用setAdapter填充数据。还探讨了ListView的优化技巧,如利用convertView减少findViewById操作,以及如何为Item设置点击事件。
摘要由CSDN通过智能技术生成

ListView效果如下,显示的是多个布局的一个列表,其中每个布局可进行自定义。
代码见下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

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

</LinearLayout>

一、实现ListView

1、自定义ListView里的小条目Item

这块为了方便起见,Item里面只写一个TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textview"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </TextView>

</LinearLayout>

2、模拟接收到一些要显示的数据

自定义Bean类,作为接收到单个数据的实体类

package com.example.seconddemo_listview;

public class Bean {
   

    private String name;

    public Bean(String name){
   
        this.name = name;
    }

    public String getName
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值