ListView 列表视图Ⅰ基本属性和简单适配器

ListView是应用最为广泛的数据显示视图

ListView的基本属性

 

 

实现ListView的一般步骤

   · 在主句文件中编写代码(xml) 

     --- 添加ListView标签

   · 在Activity中编写代码(java)   

     --- 获取ListView对象

     --- 准备数据源

     --- 配置适配器

     --- 将适配器关联到ListView

使用ArrayAdapter实现文字列表

   · 在布局文件中编写代码(xml)

     --- 添加ListView标签(main.xml)

   · 在Activity中编写代码(java)

     --- 获取ListView对象

     --- 准备数据源(Array数组)

     --- 配置适配器(ArrayAdapter)

     --- 将适配器关联到ListView

示例1:

Java代码

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main7);

        //1.拿到listview对象
        ListView lv = (ListView) this.findViewById(R.id.lv_main);

        //2.数据源
        String[] data = {
                 "初识Android",
                 "开发环境搭建",
                 "基础控件Ⅰ",
                 "基础控件Ⅱ",
                 "线性布局",
                 "相对布局"
                 };


        //3.设置适配器
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                data
        );

        //4.关联适配器
        lv.setAdapter(adapter);

    }

}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <!--listview的标签-->
    <!--
        android:divider="#f00"  下划线
        android:dividerHeight="2px"
        android:scrollbars="none"  滚动条
        android:fadeScrollbars="false"

        android:entries="@array/names"
        -->
    <ListView
        android:id="@+id/lv_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />

</RelativeLayout>

values -- New -- XML -- Values XML File 并命名为 arrays

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="names">
        <item>Tom</item>
        <item>Jerry</item>
        <item>Jack</item>

    </string-array>
    
    
    
</resources>

使用SimpleAdapter实现图文混编列表

   · 在布局文件中编写代码

     --- 添加ListView标签(main.xml)

     --- 编写行布局文件(item.xml)

   · 在Activity中编写代码

     --- 获取ListView对象

     --- 准备数据源(List<Map>)

     --- 配置适配器(SimpleAdapter)

     --- 将适配器关联到ListView

意前后id的一致性

示例2:

Java代码

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main7);

        //1.拿到listview对象
        ListView lv = (ListView) this.findViewById(R.id.lv_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.ic_10);
        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.ic_2);
        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.ic_4);
        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.ic_15);
        map.put("title", "捕鱼达人2");
        map.put("version", "版本: 2.3.0");
        map.put("size", "大小: 45.53M");
        list.add(map);

        //3.设置适配器
          SimpleAdapter adapter = new SimpleAdapter(
                  this,
                  list,
                  R.layout.item,
                  new String[]{"logo","title","version","size",},
                  new int[]{R.id.logo,R.id.title,R.id.version,R.id.size}

          );

        //4.关联适配器
         lv.setAdapter(adapter);

    }

}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <!--listview的标签-->
    <!--
        android:divider="#f00"  下划线
        android:dividerHeight="2px"
        android:scrollbars="none"  滚动条
        android:fadeScrollbars="false"

        android:entries="@array/names"
        -->
    <ListView
        android:id="@+id/lv_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants"
    >

    <RelativeLayout
        android:id="@+id/box"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <ImageView
            android:id="@+id/logo"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@mipmap/book"
            android:layout_centerVertical="true"/>

        <Button
            android:id="@+id/btn"
            android:layout_width="66dp"
            android:layout_height="30dp"
            android:text="卸载"
            android:textSize="14sp"
            android:textColor="#fff"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@drawable/btn_selector"
            />

        <RelativeLayout
            android:id="@+id/text_box"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/logo"
            android:layout_toLeftOf="@id/btn"
            android:layout_centerVertical="true"

            >

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="记事本"
                android:textSize="16sp"
                android:textColor="#000"

                />

            <TextView
                android:id="@+id/version"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="版本号: 1.0"
                android:textSize="12sp"
                android:textColor="#999"
                android:layout_centerVertical="true"
                />

            <TextView
                android:id="@+id/size"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="大小: 3.0 M"
                android:textSize="12sp"
                android:textColor="#999"
                android:layout_alignParentBottom="true"

                />

        </RelativeLayout>



    </RelativeLayout>


</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/from_heat/article/details/81298293
今日推荐