Android UI之ListView

本文介绍了Android开发中常用的ListView组件,探讨了xml布局文件在ListView中的应用,详细解析了BaseAdapter的工作原理及缓存机制。通过示例展示了如何使用xml文件配置ListView的item布局,并解释了BaseAdapter如何将数据源与界面显示绑定,实现数据和显示的解耦,同时讲解了ListView的缓存策略以提高性能。
摘要由CSDN通过智能技术生成

简介:ListView组件是在Android APP开发中经常用到的组件,主要是用来显示列表以及相应按下触发完成界面的跳转。

xml文件在ListView中的使用:

xml的作用:xml文件在Android开发中往往承担了界面的布局的作用,其中包含了各个组件以及它们在界面上存储的位置;同时比如strings.xml文件可以配置activity中用到的字符串,dimens.xml文件可以配置边界的位置参数。MainActivity也有属于它的布局文件,其中ListView是其中的一个组件。xml文件类似于给具体内容的实现框定了一个格式,它规定了ListView中数据填充的形式,实现了M(模型)和V(视觉)的分离。
activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.asynctask.MainActivity" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        android:text="@string/title1" />

    <!-- ListView 由item.xml里的TextView给值 。-->
    <ListView
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
   />
</RelativeLayout>

效果图
在这里插入图片描述
分析:布局文件的ListView中第一行代码为它申请了唯一的id标识,第二、三行代码表示它的大小可以自适应内容的大小而变化,第四行体现了相对布局的特点,ListView放在文本textView的下方。ListView中的item的格式在哪里约束呢?同理,为其中的item自然也需要相应的xml文件来进行配置,在layout文件加下新建一个xml文件,如下:
item.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="horizontal"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值