Day8、ListView和适配器Adapter三

来源:互联网 发布:multisim单片机继电器 编辑:程序博客网 时间:2024/06/05 09:20

一、为什么要学习自定义Adapter

1.ArrayAdapter:适应于简单的位子列表。

2.SimpleAdapter:适应于简单的图文混搭列表,但不适宜驾驭复杂的业务逻辑。

3.自定义Adapter:适用于绝大多数情况。

二、自定义Adapter的基本步骤

1.继承BaseAdapter

2.实现getView方法(重写此方法,每行的布局具体填充什么数据)

3.关联ListView

三、用自定义适配器的方法来实现Day7中的效果,直接贴码

1.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="match_parent"    android:background="#ffc"     android:descendantFocusability="blocksDescendants">    <!-- 当点击某一行时,没有背景颜色的变化 ,即item项不被选中的问题 原因:行中包含了按钮,按钮抢夺了焦点 解决:夺回焦点在行布局中设置descendantFocusability属性-->    <!-- 行布局 -->    <RelativeLayout         android:id="@+id/box"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="5dp"        android:layout_marginLeft="5dp"        android:layout_marginRight="5dp"        android:padding="5dp">        <ImageView             android:id="@+id/iv_logo"            android:layout_width="70dp"            android:layout_height="70dp"            android:src="@drawable/deng"            android:layout_centerVertical="true"/>        <Button             android:id="@+id/btn"            android:layout_width="wrap_content"            android:layout_height="32dp"            android:text="@string/btn_az"            android:textColor="#fff"            android:textSize="14sp"            android:background="@drawable/btn_gn_n"            android:layout_centerVertical="true"            android:layout_alignParentRight="true"/>        <!-- 文字说明部分 -->        <LinearLayout             android:id="@+id/textbox"            android:layout_width="match_parent"            android:layout_height="70dp"            android:orientation="vertical"            android:layout_toRightOf="@id/iv_logo"            android:layout_toLeftOf="@id/btn">            <TextView                android:id="@id/title"               android:layout_width="match_parent"               android:layout_height="wrap_content"               android:text="@string/text_t"               android:textSize="16sp"               android:textColor="#000"               android:layout_marginTop="3dp"/>            <TextView                android:id="@+id/version"               android:layout_width="match_parent"               android:layout_height="wrap_content"               android:text="@string/text_v"               android:textSize="12sp"               android:textColor="#00f"               android:layout_marginTop="6dp"/>            <TextView                android:id="@+id/size"               android:layout_width="match_parent"               android:layout_height="wrap_content"               android:text="@string/text_s"               android:textSize="12sp"               android:textColor="#00f"               android:layout_marginTop="5dp"/>        </LinearLayout>    </RelativeLayout></RelativeLayout>

2.listview.xml

<?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:orientation="vertical" >    <ListView         android:id="@+id/lv_play"        android:layout_width="match_parent"        android:layout_height="wrap_content"/></LinearLayout>

3.自定义的适配器(MyAdapter.java)

package com.oldtogether.adapterdemo2;import java.util.List;import java.util.Map;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class MyAdapter extends BaseAdapter {    List<Map<String,Object>> list;    LayoutInflater Inflater;//反射器    //初始化反射器    public MyAdapter(Context context) {        this.Inflater=LayoutInflater.from(context);    }    public void setList(List<Map<String, Object>> list) {        this.list = list;    }    @Override    public int getCount() {        //获取ListView的行数        return list.size();    }    @Override    public Object getItem(int index) {        //获得Item项的对象,其中的参数为此Item的下标,从零开始        return list.get(index);    }    @Override    public long getItemId(int id) {        //获得ItemId        return id;    }    @Override    public View getView(int position, View arg1, ViewGroup arg2) {        /*         * 在模拟器上看似ListVie每一项是同时出现的,其实并不是,它是每行依次渲染的,最终return         * 1、由于View在上下文中没有应用,所以之所以在上面定义反射器,完全是为了在这里反射出一个对象         * 进而获得大题的围观框架         * 2、获得其他属性,即此行布局中的图片部分,文字说明部分,按钮部分         * 3、注意:此类是MyAdapter,在findViewById时候,需要view点出(其实在MainActivity中是通过         * this.findViewById点出的,但这里需要View对象点出)         * 4、获得控件对象         * 5、通过控件对象调用方法填充数据         */        View view =Inflater.inflate(R.layout.item, null);//获得每行的大体外观框架        //获得对象        ImageView logo=(ImageView) view.findViewById(R.id.iv_logo);        TextView title=(TextView) view.findViewById(R.id.title);        TextView version=(TextView) view.findViewById(R.id.version);        TextView size=(TextView) view.findViewById(R.id.size);        //添加数据        Map map=list.get(position);        logo.setImageResource((int) map.get("logo"));        title.setText((String)map.get("title"));        version.setText((String)map.get("version"));        size.setText((String)map.get("size"));        return view;    }}

4.MainActivity.java

package com.oldtogether.adapterdemo2;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity2 extends ActionBarActivity {    // 创建数组,采用for循环进行遍历    private int[] imageIds = new int[] { R.drawable.daxiang, R.drawable.maozi, R.drawable.nangua, R.drawable.nanguobq,            R.drawable.tiaopi, R.drawable.xiaolian, R.drawable.xin,R.drawable.weixin,R.drawable.hongx,R.drawable.xm };    private String[] titles = new String[] { "卖萌大象", "圣诞帽子", "愤怒南瓜", "难过表情", "调皮表情", "我倩的笑脸表情", "oldtogether爱心","微信","粉色少女心","一代撸" };    private String[] versions = new String[] { "版本:1.10", "版本:2.10", "版本:1.11", "版本:2.12", "版本:3.10", "版本:2.20","版本:5.20","版本:1.23","版本:2.21","版本:2.40", };    private String[] sizes = new String[] { "大小:32.01M", "大小:32.02M", "大小:23.01M", "大小:11.01M", "大小:41.01M","大小:11.01M","大小:33.01M","大小:33.01M","大小:33.01M","大小:33.01M",};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.listview);        // 1、过的ListView对象        ListView lv = (ListView) findViewById(R.id.lv_play);        // 2、数据源        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();        for (int i = 0; i < titles.length; i++) {            Map<String, Object> map = new HashMap<String, Object>();            map.put("logo", imageIds[i]);            map.put("title", titles[i]);            map.put("version", versions[i]);            map.put("size", sizes[i]);            list.add(map);        }        // 3、设置适配器        MyAdapter adapter = new MyAdapter(this);        adapter.setList(list);//传入数据        // 4、关联适配器        lv.setAdapter(adapter);    }}

四、心得总结

1.自定义适配器是应用最广泛的是适配器,需要基础BaseAdapter并重写方法。

2.自定义的核心方法是getView()方法,在该方法中通过LayoutInflater反射出行布局对象,并填充上数据内容。

3.问题:创建对象的两种方法????

0 0