【Android】14、SimpleAdapter

来源:互联网 发布:转行it 知乎 编辑:程序博客网 时间:2024/06/05 09:22

1、使用SimpleAdapter实现图文混编

在布局文件中编写代码

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

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

2、在Activity中编写代码

——获取ListView对象

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

——配置适配器(SimpleAdapter)

——将适配器关联到ListView

3、实例。

————————item.xml

<?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="wrap_content"
    android:descendantFocusability="blocksDescendants">
    <ImageView
        android:id="@+id/logo"
        android:src="@mipmap/qq"
        android:padding="5dp"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        />
    <Button
        android:id="@+id/button"
        android:text="卸载"
        android:textColor="#FFF"
        android:background="@mipmap/btn"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"/>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/logo"
        android:layout_toLeftOf="@id/button">
        <TextView
            android:id="@+id/title"
            android:text="QQ"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"/>
        <TextView
            android:id="@+id/version"
            android:text="版本:1.0"
            android:textColor="#aaa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"/>
        <TextView
            android:id="@+id/size"
            android:text="空间:12M"
            android:textColor="#aaa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/version"/>
    </RelativeLayout>
</RelativeLayout>


———————————main.xml

<?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="wrap_content">
    <ListView
        android:id="@+id/lv_soft"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</RelativeLayout>


————————————MainActivity.java

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


        //2,数据源
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("logo",R.mipmap.qq);
        map.put("title","QQ");
        map.put("version","版本:1.0");
        map.put("size","空间:32M");
        list.add(map);


        map=new HashMap<String,Object>();
        map.put("logo",R.mipmap.weixin);
        map.put("title","微信");
        map.put("version","版本:2.2");
        map.put("size","空间:12M");
        list.add(map);


        map =new HashMap<String,Object>();
        map.put("logo",R.mipmap.qzone);
        map.put("title","QQ空间");
        map.put("version","版本:4.3");
        map.put("size","空间:18M");
        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,关联ListView
        lv_soft.setAdapter(adapter);


4、ListView行布局SimpleAdapter无法获得焦点解决办法。

因为Button按钮有“抢”焦点的行为,在行布局的最外层容器加下列代码即可

android:descendantFocusability="blocksDescendants"

这样可使得按到行布局的时候,有颜色改变。


0 0