Android study notes 21: commonly used controls-list view

Zero, learning goals

  1. Can tell the basic usage of the list control
  2. Can name the four adapters used by the list control
  3. Can write simple Android applications using list controls

1. Overview of List View

1. Inheritance diagram

Insert picture description here

2. Four elements of list view

(1) List control
(2) Adapter
(3) Data source
(4) List item template
Insert picture description here

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
    Insert picture description here

(2) Operation effect

(3) Involving knowledge points

  1. Linear layout (LinearLayout)
  2. Label (TextView)
  3. Button
  4. List View (ListView)
  5. Array Adapter (ArrayAdapter)
  6. Array or Array List (Array | ArrayList)

(4) Implementation steps

1. Create an Android application [ReadAncientPoetry]

Insert picture description here
Insert picture description here

2. Copy the background picture to the drawable directory

Insert picture description here

3. Layout resource file activity_main.xml

Insert picture description here

<?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

Insert picture description here

<?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

Insert picture description here

<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

Insert picture description here

  • Declare variable
    Insert picture description here

  • Get control instance by resource identifier
    Insert picture description here

  • Initialize the ancient poem title array
    Insert picture description here

  • Create an array adapter
    Insert picture description here

  • The array adapter created above uses the list item template resources provided by the platform- anddroid.R.layout.simple_list_item_1
    Insert picture description here

  • Set the adapter for the list control
    Insert picture description here

7. Start the application and check the effect

  • This is the effect of using the list item template provided by the platform
    Insert picture description here
  • Below we use a custom list item template poem_list_item.xml
    Insert picture description here
  • Start the application and check the effect
    Insert picture description here
  • One page does not display all the titles of ancient poems, you can swipe to see the rest of the list items
    Insert picture description here
  • Slide list control effect demo
    Insert picture description here

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
    Insert picture description here
  • 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

Insert picture description here

10. Create an interface that displays the content of ancient poems-ContentActivity

  • Create ContentActivity based on template
    Insert picture description here
    Insert picture description here

11. Content interface layout resource file content_activity.xml

Insert picture description here

<?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>                                                                           

Guess you like

Origin blog.csdn.net/howard2005/article/details/109461197