一、了解 AdapterView 及其子类  

android 子view强制获取事件 安卓view的子类_android

(这个图片是网上找的)主要是了解一下结构,其中AbsListView、AbsSpinner、AdapterViewAnimation依然是抽象类,实际使用时需要使用它们的子类。后面我会逐个练习这些子类,比较这些子类实现效果和使用的区别。

二、了解Adapter接口及实现类

android 子view强制获取事件 安卓view的子类_listview_02

其中ListAdapter为AbsListView提供列表项,而SpinnerAdapter为AbsSpinner提供列表项。从图中可以看出,几乎所有Adapter都继承了BaseAdapter,而BaseAdapter同时实现了AbsListView、SpinnerAdapter两个接口。

(需要较灵活的自定义效果的时候BaseAdapter确实非常有用啊~~~)

Adapter常用的实现类如下:

1、ArrayAdapter:简单易用,通常用于将数组或List集合的多个值包装成多个列表项,通俗点说,也就是直接将数组或list集合中的内容直接包装成ArrayAdapter.

例如:

String[] array=new String[]{
"周杰伦","方文山","青花瓷"
};
ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this,R.layout.xxx,array);
listXXX.setAdapter(adapter1);

这是arrayadapter构造方法中常用的一个。其中第一个参数是Context:代表访问整个Android应用的接口;第二个参数是textViewResourceId:这个资源ID可以在xml文件中自定义,表示列表项的外观;第三个参数是数组或List.

2.SimpleAdapter:可用于将List集合的多个对象包装成多个列表项。最常见的形式就类似于QQ的联系人列表,由头像,姓名,信息组成了一个列表项。这就扩展了arrayadapter列表项只能为textview的形式,功能比较强大。

例如:

首先是设置两个布局文件:(activity_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/list1"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content" />

</LinearLayout>

list_item_style.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="horizontal" >

    <ImageView
        android:id="@+id/header1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <TextView 
            android:id="@+id/name1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:textColor="#f0f"
            android:paddingLeft="10dp"
            />
        <TextView 
            android:id="@+id/desc1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14dp"
            android:paddingLeft="10dp"/>
        </LinearLayout>

</LinearLayout>

Java 文件中主要是将需要显示的数据封装在Map对象,然后以list集合的形式放进适配器:

public class Practise extends Activity {

	private String[] name = new String[] { "孙悟空", "唐僧", "猪八戒" };
	private String[] desc = new String[] { "一只猴子", "一个和尚", "一只猪" };
	private int[] headers = new int[] { R.drawable.x, R.drawable.y,
			R.drawable.z };

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_listview);
		List<Map<String, Object>> listitems = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < name.length; i++) {
			Map<String, Object> listitem = new HashMap<String, Object>();
			listitem.put("header", headers[i]);
			listitem.put("name", name[i]);
			listitem.put("desc", desc[i]);
			listitems.add(listitem);
		}
		SimpleAdapter sp = new SimpleAdapter(this, listitems,
				R.layout.list_item_style, new String[] { "name", "header",
						"desc" }, new int[] { R.id.name1, R.id.header1,
						R.id.desc1 });
		ListView ls=(ListView)findViewById(R.id.list1);
		ls.setAdapter(sp);
	}

}

从上面可以发现SimpleAdapter()有5个参数:第一个参数是context,第二个参数是list集合,第三个参数是指定界面布局的ID,即我们定义的列表项风格的xml文件的ID,第四个参数是决定Map<>对象中哪些key对应的value来生成列表项,第五个参数是决定填充哪些组件,也就是我们定义的xml文件中相应的组件的id.

效果如下:

android 子view强制获取事件 安卓view的子类_listview_03


3.BaseAdapter:通常用于被扩展,可以对各列表项进行最大限度的定制。

重点是重写如下4个方法:

getCount():该方法的返回值Adapter 将会包含多少个列表项;
getItem(int position):该方法的返回值决定第position处的列表项的内容;
getItemId(int position):决定第position 处的列表项的ID;
getView(int position,View convertView,ViewGroup parent):返回的View将作为列表框;(感觉这个方法最重要)

4.SimpleCursorAdapter:与SimpleAdapter基本相似,但是用于包装Cursor 提供的数据。(这个我暂时还没有用到过(⊙﹏⊙))

(以上是比较常用的4类adapter实现类)

 除了使用xml文件布局外,还可以直接使用ListActivity等Activity的继承类,省了布局文件,但是adapter实现类部分和使用普通activity是一样的。注意在ListActivity中使用  setListAdapter(adapter);

三、测试使用主要的AdapterView 组件

1.ListView上面已经使用过了。

2.ExpandableListView(可展开的列表组件),ExpandableListView所显示的列表项应该由ExpandableListAdapter提供,它的一个重要子类是BaseExpandableListAdapter,具体的使用方法可以去参考api,用法与普通ListView非常相似。

扩展BaseExpandableListAdapter时关键是要实现4个方法:

getGroupCount():返回包含的组列表项的数量;

getGroupView():返回View对象作为组列表项;(外观设计)

getChildCount():返回特定组所包含的子列表项的数量;

getChildView();返回View对象作为特定组子列表项;(外观设计)

在布局文件中<ExpandableListView.../>中比较常用的特性有:android:childIndicator/android:groupIndicator(显示在子/组列表项旁边的Drawable对象)虽然这样设置很方便,但是我不知道怎么才能控制图像的大小?

3.Spinner组件 其实就是一个简单的列表选择框,在使用这个组件的时候我们常常是已经确定了需要显示的内容,所以在使用的时候不需要考虑到动态获取,只需要直接为其指定 android:entries属性即可实现Spinner.(在res/value目录下已经定义了数组资源文件);或者在java代码中用数组创建arrayadapter对象。

4.Gallery组件 (额。。。Android已经不再推荐使用这个组件了,建议使用HorizontalScrollView和ViewPager代替)

5.AdapterViewFipper和StackView 有动画效果,继承自AdapterViewAnimator