Android开发基础控件ListView的使用

1、ListView继承自ViewGroup,即本身也是个View,直接在xml中添加即可;
2、ListView需要ListAdapter提供数据源,Adapter决定每一行的样式与数据;
3、以最简单的ArrayAdapter为例,如下图所示:

4、示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.yusian.listview;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        ListView listView = (ListView)findViewById(R.id.lv_main);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
        adapter.add("Sian");
        adapter.add("Tom");
        adapter.add("Lily");
        adapter.add("Jim");
        listView.setAdapter(adapter);
    }
}

3 thoughts on “Android开发基础控件ListView的使用

  1. Pingback: Android开发之ExpandableListView(分组列表)的简单使用 | 小龙虾博客 (Crayfish)

  2. Sian Post author

    如果是BaseAdapter同样实现上述效果,则需要新建一个BaseAdapter实体类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    
    package com.yusian.listview;
     
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
     
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
     
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
     
    public class MainActivity extends AppCompatActivity {
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            ListView listView = (ListView)findViewById(R.id.lv_main);
            final List<Person> array = new ArrayList<>();
            for (int i = 0; i < 20; i++) {
                Person p = new Person(R.mipmap.ic_launcher, "Name--> "+i);
                array.add(p);
            }
     
            BaseAdapter adapter = new BaseAdapter() {
                @Override
                // 返回ListView的Cell数
                public int getCount() {
                    return array.size();
                }
     
                @Override
                // 数据源对象
                public Object getItem(int position) {
                    return array.get(position);
                }
     
                @Override
                // 默认直接给position值即可
                public long getItemId(int position) {
                    return position;
                }
     
                @Override
                // 单个Cell的具体长相,这里涉及到重用机制,系统会将复用的View回传;
                public View getView(int position, View convertView, ViewGroup parent) {
                    if (convertView == null){
                        convertView = LayoutInflater
                                .from(parent.getContext())
                                .inflate(R.layout.item_cell, parent, false);
                    }
     
                    Person p = array.get(position);
                    ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_image);
                    TextView textView = (TextView) convertView.findViewById(R.id.tv_title);
                    imageView.setImageResource(p.getIcon());
                    textView.setText(p.getName());
                    return convertView;
                }
            };
            listView.setAdapter(adapter);
        }
    }
     
    class Person{
        private int icon;
        private String name;
     
        public Person(int icon, String name) {
            this.icon = icon;
            this.name = name;
        }
     
        public int getIcon() {
            return icon;
        }
     
        public String getName() {
            return name;
        }
    }

    效果图:

  3. Sian Post author

    SimpleAdapter相对要复杂一点点,因为没有直接使用系统自带的布局,需要自定义布局文件,例如:
    XML

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    
    package com.yusian.listview;
     
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
     
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
     
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
     
    public class MainActivity extends AppCompatActivity {
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            ListView listView = (ListView)findViewById(R.id.lv_main);
            // 数据源,HashMap的数组集合,一张图片和一个标签
            List<HashMap<String, Object>> array = new ArrayList<>();
            for (int i = 0; i < 9; i++) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("image", R.mipmap.ic_launcher);
                map.put("title", "Title - "+i);
                array.add(map);
            }
            // SimpleAdapter构造函数需要传入五个参数
            // 第一个参数与ArrayAdapter一样,传入当前上下文;
            // 第二个参数传入数组集合;
            // 第三个参数传入自定义布局文件;
            int itemLayout = R.layout.item_cell;
            // 第四个参数输入数组集合当中HashMap中的key集合;
            String[] keys = {"image", "title"};
            // 第五个参数输入自定义布局中控件的id
            int[] items = {R.id.iv_image, R.id.tv_title};
            SimpleAdapter adapter = new SimpleAdapter(this, array, itemLayout, keys, items);
            listView.setAdapter(adapter);
        }
    }

    Activity

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    
    package com.yusian.listview;
     
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
     
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
     
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
     
    public class MainActivity extends AppCompatActivity {
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            ListView listView = (ListView)findViewById(R.id.lv_main);
            // 数据源,HashMap的数组集合,一张图片和一个标签
            List<HashMap<String, Object>> array = new ArrayList<>();
            for (int i = 0; i < 9; i++) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("image", R.mipmap.ic_launcher);
                map.put("title", "Title - "+i);
                array.add(map);
            }
            // SimpleAdapter构造函数需要传入五个参数
            // 第一个参数与ArrayAdapter一样,传入当前上下文;
            // 第二个参数传入数组集合;
            // 第三个参数传入自定义布局文件;
            int itemLayout = R.layout.item_cell;
            // 第四个参数输入数组集合当中HashMap中的key集合;
            String[] keys = {"image", "title"};
            // 第五个参数输入自定义布局中控件的id
            int[] items = {R.id.iv_image, R.id.tv_title};
            SimpleAdapter adapter = new SimpleAdapter(this, array, itemLayout, keys, items);
            listView.setAdapter(adapter);
        }
    }

    效果图:

Leave a Reply