Android_ListView

目录

  一、ListView的常用属性

  二、实现ListView的一般步骤

  三、适配器的种类

  四、ArrayAdapter适配器实现简单文字列表

  五、SimpleAdapter实现图文混编列表

 

 

一、ListView的常用属性

divider 设置分割线的颜色
dividerHeight 设置分割线的高度
scrollbars 设置滚动条的隐藏或显示
fadeScrollbars 设置滚动条的自动隐藏或显示
<?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">

    <ListView
        android:id="@+id/list_view"
        android:dividerHeight="10px"
        android:divider="#000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
View Code

 

二、实现ListView的一般步骤

1.在布局文件中编写代码(xml)
  --添加ListView标签
2.在Activity中编写代码(java)
  --获取ListView对象
  --准备数据源
  --配置适配器
  --将适配器关联到ListView

 

三、适配器的种类

ArrayAdapter 适用于简单的文字列表
SimpleAdapter 适用于简单的图文混搭列表
SimpleCursorAdapter 适用于数据源是数据库的列表
自定义Adapter(继承BaseAdapter) 最灵活的适配器,适用于绝大多数情况

 

 

四、ArrayAdapter适配器实现简单文字列表

1.在布局文件中添加ListView标签(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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.ryan.test.MainActivity">

    <ListView
        android:id="@+id/list_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
View Code

2.在Activity中编写代码

//1.获取ListView对象
ListView list_main = (ListView) findViewById(R.id.list_main);

//2.准备数据源
String[] data = {
        "初识Android",
        "开发环境搭建",
        "基础控件"
};

//3.准备适配器Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        this,
        android.R.layout.simple_list_item_1,//行布局,系统自带布局
        data
        );

//4. 将适配器关联到ListView
list_main.setAdapter(adapter);
View Code

 

 

五、SimpleAdapter实现图文混编列表

1.在布局文件中添加ListView标签(main.xml)

2.编写行布局文件(item.xml)(id作为识别标识)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:padding="5dp"
        android:layout_margin="5dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">
        <ImageView
            android:id="@+id/logo"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/danger"
            />
        <LinearLayout
            android:layout_weight="1"
            android:orientation="vertical"
            android:layout_marginLeft="5dp"
            android:padding="5dp"
            android:layout_width="0dp"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/title"
                android:layout_marginTop="3dp"
                android:textSize="18sp"
                android:text="危险品"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/version"
                android:layout_marginTop="3dp"
                android:textSize="12sp"
                android:text="版本:1.0"
                android:textColor="#999"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/size"
                android:layout_marginTop="3dp"
                android:textSize="12sp"
                android:text="占用空间:20M"
                android:textColor="#999"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
            <Button
            android:text="卸载"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@drawable/btn_selector"
            android:textColor="#fff"
            android:textSize="18sp"
            />
    </LinearLayout>
</LinearLayout>
item.xml

 

3.在Activity中编写代码

        //1.获取ListView对象
        ListView list_main = (ListView) findViewById(R.id.list_main);



        //2.数据源
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("logo", R.drawable.danger);
        map.put("title", "千千静听");
        map.put("version", "版本: 8.4.0");
        map.put("size", "大小: 32.81M");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("logo", R.drawable.danger);
        map.put("title", "时空猎人");
        map.put("version", "版本: 2.4.1");
        map.put("size", "大小: 84.24M");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("logo", R.drawable.danger);
        map.put("title", "360新闻");
        map.put("version", "版本: 6.2.0");
        map.put("size", "大小: 11.74M");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("logo", R.drawable.danger);
        map.put("title", "捕鱼达人2");
        map.put("version", "版本: 2.3.0");
        map.put("size", "大小: 45.53M");
        list.add(map);



        //3. 准备适配器Adapter
        SimpleAdapter adapter = new SimpleAdapter(
                this,//上下问
                list,//数据源
                R.layout.item,//自定义行布局
                new String[]{"logo","title","version","size"},//Map中的key
                new int[]{R.id.logo,R.id.title,R.id.version,R.id.size}//行布局中的id
        );

        //4. 将适配器关联到ListView
        list_main.setAdapter(adapter);
View Code

posted @ 2017-03-10 16:22  Core丨  阅读(166)  评论(0编辑  收藏  举报