Android -列表视图ListView 基本适配器BaseAdapter具体使用方法(附参考代码)

我最近做项目用到了基本适配器BaseAdapter,所以写篇博客总结一下,希望也能对你有所帮助

什么时候用BaseAdapter(同一项存在多个控件,复杂的列表时)

Android中Adapter类其实就是把数据源绑定到指定的View上,然后再返回该View,而返回来的这个View就是ListView中的某一行item。这里返回来的View正是由我们的Adapter中的getView方法返回的。这样就会容易理解数据是怎样一条一条显示在ListView 中的。

简单的ListView可使用ArrayAdapter和SimpleAdapter两种适配器。其中, ArrayAdapter适用于纯文本的列表数据,SimpleAdapter适用于带图标的列表数据。实际应用中常常有更复杂的列表,比如同一项中存在多个控件,这种情况即使用SimpleAdapter也很吃力,而且不易扩展。基于此, Android提供了一种适应性更强的基本适配器BaseAdapter,该适配器允许开发者在别的代码文件中进行逻辑处理,大大提高了代码的可读性和可维护性。

step1:创建列表单行的布局

新建一个layout,和普通的layout文件差不多,只不过这个文件设置的是ListView单项的样式。

例如:item_layout.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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textview_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:visibility="visible" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/image_time"
            android:layout_width="wrap_content"
            android:layout_height
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值