Android 开发之 ListView 使用入门

当需要展示一些条目数据,如下图的新闻主页界面,每一个条目的布局格式相同,比如下图 ,每个条目包含4个元素,分别是“新闻标题”,“发布时间”,“跟帖数量”,“新闻截图”。而且这种条目是可以上下滚动的,可以不断刷新。

当有以上需求是我们就会考虑使用ListView控件来实现。可分以下几个步骤来实现:

一 . 构建item元素

ListView 可以理解成一个立起来的,每层间隔等距的储物架,只不过每一层放的东西都是类似的。那么第一步我们就需要先构建要放的东西(item)。

首先在layout 文件中构建一个item.layout文件

// ###### item_layout #########

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:padding="10dp">

<TextView

android:id="@+id/tv_news_title"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginRight="120dp"

android:layout_marginBottom="10dp"

android:maxLines="2"

android:text="牺牲林草局长杨达瓦:带儿看病只能住一天10元旅馆"

android:textColor="#000"

android:textSize="18sp" />

<TextView

android:id="@+id/tv_news_time"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/tv_news_title"

android:text="1小时前"

android:textColor="#6e6e6e"

android:textSize="12sp" />

<TextView

android:id="@+id/tv_news_read"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/tv_news_title"

android:layout_marginLeft="20dp"

android:layout_toRightOf="@id/tv_news_time"

android:text="0 跟帖"

android:textColor="#6e6e6e"

android:textSize="12sp" />

<ImageView

android:id="@+id/iv_picture"

android:layout_width="100dp"

android:layout_height="80dp"

android:layout_alignParentRight="true"

android:src="@drawable/man" />

</RelativeLayout>

预览效果图如下:

二. 在父布局中新建ListView

// ##### activity_main.layout #####

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

<ListView

android:id="@+id/lv"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

</ListView>

</LinearLayout>

上面这段代码只是在父布局中插入了ListView控件,里面还没有填充内容。

三. 新建一个listview Adapter

根据安卓开发的模块化分包规则,我们新建一个adapters 包,以后能用到的adapter都会放到这个包里,这样显得项目结构清晰明了。目录结构如下图:

ListViewAdapter 继承自BaseAdapter。BaseAdapter是一个抽象类,需要实现4个默认方法,分别是:

其中getItem()方法和getItemId()方法,我们暂时用不上,可以不用理会,重点需要关注的是getCount() 和 getView() 方法。顾名思义,getCount() 方法返回的是一个int型数据,代表我们当前的ListView有多少条数据,也就是说代表我们的“储物架” 有多少层。一般会从外界传入一个List, 采用List.size()来指定这个ListView的长度。而getView() 方法返回的则是也一个view对象,也就是说在这个方法里,我们需要把前面写的item.layout 封装成一个view对象,放到ListView控件中去展示。

重点看看getView()方法如何实现:

最简单的实现就是调用View的静态inflate方法,直接填充布局文件,生成一个view对象。注意第一个参数mContext 需要在构造函数中传入。

但是上面这种写法会有个问题,注意看getView()方法的第二个参数:convertView。

看API介绍:一个老的可重用的view对象,如果可能的话,你在使用它之前,必须检查这个view是非空且是正确的类型。

上面这段话的意思就是说,如果它不为空,则继续重用,若为空,则生成新的view。

所以最终的代码如下:

四. 在主函数中设置adapter

注意在adapter的构造函数中传入当前类的context。

猜你喜欢

转载自blog.csdn.net/yifei325325/article/details/89026884