Article Directory
- Zero, learning goals
- 1. Overview of List View
- 2. List view case based on array adapter-read ancient poems
-
- (1) Array adapter
- (2) Operation effect
- (3) Involving knowledge points
- (4) Implementation steps
-
- 1. Create an Android application [ReadAncientPoetry]
- 2. Copy the background picture to the drawable directory
- 3. Layout resource file activity_main.xml
- 4. Poem list item template poem_list_item.xml
- 5. String resource file strings.xml
- 6. Main interface class-MainActivity
- 7. Start the application and check the effect
- 8. Click the list item, toast will pop up, showing the name and number of the ancient poem
- 9. Start the application and check the effect
- 10. Create an interface that displays the content of ancient poems-ContentActivity
- 11. Content interface layout resource file content_activity.xml
Zero, learning goals
- Can tell the basic usage of the list control
- Can name the four adapters used by the list control
- Can write simple Android applications using list controls
1. Overview of List View
1. Inheritance diagram
2. Four elements of list view
(1) List control
(2) Adapter
(3) Data source
(4) List item template
3. Four adapters
- List View (ListView), which is the grandchild of AdapterView, uses the adapter as a bridge to bind the data source.
- There are four types of adapters available: ArrayAdapter, SimpleAdapter, SimpleCursorAdapter, BaseAdapter.
2. List view case based on array adapter-read ancient poems
(1) Array adapter
- The array adapter has two overloaded construction methods-the difference lies in the third parameter, one is an object array and the other is an object list
(2) Operation effect
(3) Involving knowledge points
- Linear layout (LinearLayout)
- Label (TextView)
- Button
- List View (ListView)
- Array Adapter (ArrayAdapter)
- Array or Array List (Array | ArrayList)
(4) Implementation steps
1. Create an Android application [ReadAncientPoetry]
2. Copy the background picture to the drawable directory
3. Layout resource file activity_main.xml
<?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="@drawable/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>
4. Poem list item template poem_list_item.xml
<?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" />
5. String resource file strings.xml
<resources>
<string name="app_name">阅读古诗</string>
<string-array name="titles">
<item>静夜思</item>
<item>登鹳雀楼</item>
<item>相思</item>
<item>听弹琴</item>
<item>登乐游原</item>
<item>朝发白帝城</item>
<item>巴山夜雨</item>
<item>渭城曲</item>
<item>春夜喜雨</item>
<item>离离原上草</item>
<item>示儿</item>
</string-array>
<string-array name="authors">
<item>唐·李白</item>
<item>唐·王之涣</item>
<item>唐·王维</item>
<item>唐·刘长卿</item>
<item>唐·李商隐</item>
<item>唐·李白</item>
<item>唐·杜牧</item>
<item>唐·王维</item>
<item>唐·杜甫</item>
<item>唐·白居易</item>
<item>宋·陆游</item>
</string-array>
<string-array name="contents">
<item>床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。</item>
<item>白日依山尽,\n黄河入海流。\n欲穷千里目,\n更上一层楼。</item>
<item>红豆生南国,\n春来发几枝。\n愿君多采撷,\n此物最相思。</item>
<item>泠泠七弦上,\n静听松风寒。\n古调虽自爱,\n今人多不弹。</item>
<item>向晚意不适,\n驱车登古原。\n夕阳无限好,\n只是近黄昏。</item>
<item>朝辞白帝彩云间,\n千里江陵一日还。\n两岸猿声啼不住,\n轻舟已过万重山。</item>
<item>君问归期未有期,\n巴山夜雨涨秋池。\n何当共剪西窗烛,\n却话巴山夜雨时。</item>
<item>渭城朝雨浥轻尘,\n客舍青青柳色新。\n劝君更尽一杯酒,\n西出阳关无故人。</item>
<item>好雨知时节,当春乃发生。\n随风潜入夜,润物细无声。\n野径云俱黑,江船火独明。\n晓看红湿处,花重锦官城。</item>
<item>离离原上草,一岁一枯荣。\n野火烧不尽,春风吹又生。\n远芳侵古道,晴翠接荒城。\n又送王孙去,萋萋满别情。</item>
<item>死去元知万事空,\n但悲不见九州同。\n王师北定中原日,\n家祭无忘告乃翁。 </item>
</string-array>
</resources>
6. Main interface class-MainActivity
-
Declare variable
-
Get control instance by resource identifier
-
Initialize the ancient poem title array
-
Create an array adapter
-
The array adapter created above uses the list item template resources provided by the platform-
anddroid.R.layout.simple_list_item_1
-
Set the adapter for the list control
7. Start the application and check the effect
- This is the effect of using the list item template provided by the platform
- Below we use a custom list item template poem_list_item.xml
- Start the application and check the effect
- One page does not display all the titles of ancient poems, you can swipe to see the rest of the list items
- Slide list control effect demo
8. Click the list item, toast will pop up, showing the name and number of the ancient poem
- Modify the main interface class, register the item for the list control, click listener
- The parameter position is the position where the user clicks the list item, starting from 0, for example, the user clicks the third list item, then position is equal to 2, that is to say, position plus 1 is the row number
- The value of the parameter id is equal to the value of the parameter position, the two parameters are just different types
9. Start the application and check the effect
10. Create an interface that displays the content of ancient poems-ContentActivity
- Create ContentActivity based on template
11. Content interface layout resource file content_activity.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="match_parent"
android:background="@drawable/background"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginBottom="20dp"
android:layout_weight="2"
android:gravity="center_vertical"
android:textColor="#ff00ff"
android:textSize="30sp" />
<TextView
android:id="@+id/tvAuthor"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="@+id/tvContent"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginBottom="20dp"
android:layout_weight="8"
android:textColor="#0000ff"
android:textSize="20sp" />
<Button
android:id="@+id/btnBack"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="doBack"
android:text="@string/back"
android:textSize="20sp" />
</LinearLayout>