目录
列表视图4要素
- 列表控件
- 适配器(数组适配器、简单适配器、游标适配器、基适配器……)
- 数据源(数组、列表、游标……)
- 列表项模板(平台资源、用户自定义)
四种是适配器
列表视图(ListView),它是AdapterView的孙子类,要通过适配器作为梁桥来绑定数据源。
有四种适配器可以使用:
1.数组适配器(ArrayAdapter)
2.简单适配器(SimpleAdapter)
3.简单游标适配器(SimpleCursorAdapter)
4.基适配器(BaseAdapter)
古诗查看
古诗列表
1.主布局资源文件
·示例代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@mipmap/background"
android:padding="15dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/lvPoemTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#aaaaaa"
android:dividerHeight="0.5dp" />
</LinearLayout>
·实现效果
2.诗歌列表项模板
·创建模板
·示例代码
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvPoemTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="80dp"
android:textColor="#0000ff"
android:textSize="25sp" />
3.字符串资源文件
·示例代码
<resources>
<string name="app_name">阅读古诗</string>
<string-array name="titles">
<item>静夜思</item>
<item>登鹳雀楼</item>
<item>相思</item>
<item>听弹琴</item>
<item&