本文来自:安卓航班网

      ListView :在Android应用开发过程中属于最常用的系统组件之一,当然可能童鞋们问为什么会突然游戏开发中讲这个,呵呵,其实在游戏开发中,也会常常使用到系统组件,比如游戏排行榜,简单的游戏关卡选择等等,都可以来使用ListView来实现;

       当然关于ListView我想大家都会使用了,那跟大家讲解ListView是如果使用的,而是如何实现自定义一个【通用】适配器类;

       在ListView三种适配器当中,最受大家 青睐的肯定就是SimpleAdapter适配器,用过的童鞋们都很清楚,它的扩展性很强,可以将ListView中每一项都使用自定义布局,插入N多组 件;但是SimpleAdapter也有弱点,那就是当ListView中每一项有Button、CheckBox等这些有事件的组件,我们想监听它们就 必须自定义适配器!那么今天的重点也就是来讲解一下如何写一个自定义通用适配器类!

       SimpleAdapter 构造的时候,我们知道需要五个参数来进行映射数据到ListView中,那么我们今天的自定义通用适配器其实也就是实现系统SimpleAdapter的一个自定义版;

       OK,可能我说这么多,大家还是不太懂,其实今天要讲述的自定义通用适配器优点有三点:

       1.使用通用适配器就不需要每次使用自定义适配器的时候,都要去重新去写一个。
       2.构造方法与SimpleAdapter构造方法相同,五个参数也一摸一样!
       3.只需要在自定义的适配器类中,将我们需要监听的组件进行设置监听即可!别的代码不需要去改动!

       例如我们需要完成下图这种ListView:

       首先我们来完成ListView中每项的布局:

java代码:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
<ImageView
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:id
="@+id/iv"
/>
<LinearLayout
android:orientation
="vertical"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
>
<TextView
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:textSize
="20sp"
android:id
="@+id/bigtv"
/>
<TextView
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:textSize
="10sp"
android:id
="@+id/smalltv"
/>
</LinearLayout>
<Button
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="button"
android:id
="@+id/btn"
/>
<CheckBox
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:id
="@+id/cb"
/>
</LinearLayout>

原文地址:http://www.apkway.com/forum.php?mod=viewthread&tid=2675&extra=page%3D1

posted on 2011-08-10 20:11  泉と緣  阅读(607)  评论(0编辑  收藏  举报