我最近做项目用到了基本适配器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