AdapterView

 介绍过Adapter以后,让咱们来了解一下经常使用的AdapterView的使用吧。ListView 与GridView、Spinner与Gallery。他们是两组分别来自AbsListView和AbsSpinner的子类。因此会有必定的类似性。他们都须要Adapter才能将数据显示在控件上。下面会结合具体实例讲解每一个控件的用法。
java

1、ListView。将内容以列表的形式显示,是最经常使用的AdapterView控件。android

主布局文件:
ide

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>
</RelativeLayout>

效果:布局

定义布局文件items.xml用于Adapter的资源文件。this

<?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/p_w_picpathView1"
        android:layout_width="45dip"
        android:layout_height="45dip"
       />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

主Activity:spa

public class MainActivity extends Activity {
    private ListView lv;
    //定义一个adapter对象
    private SimpleAdapter adapter;
    //定义SimpleAdapter加载的数据
    private List<Map<String, Object>> list;
    private int[] imgid = { R.drawable.x1, R.drawable.x2, R.drawable.x3,
            R.drawable.x4, R.drawable.x5};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView) findViewById(R.id.listView1);
        list=new ArrayList<Map<String,Object>>();
        Map<String,Object> map;
        //实例化list
        for(int i=0;i<imgid.length;i++){
            map=new HashMap<String, Object>();
            map.put("data", "ooo"+i);
            map.put("p_w_picpath", imgid[i]);
            list.add(map);
        }
        //实例化adapter
        adapter=new SimpleAdapter(this, list, R.layout.items,
                new String[]{"data","p_w_picpath"}, new int[]{R.id.textView1,R.id.p_w_picpathView1});
        lv.setAdapter(adapter);
}
}

运行效果:3d

2、GridView。xml

在主布局文件中加入GridView。对象

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <GridView
        android:id="@+id/main_gv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="3" >
    </GridView>
</RelativeLayout>

效果:blog

定义布局文件items.xml用于Adapter的资源文件。

<?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" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <ImageView
            android:id="@+id/adapter_iv"
            android:layout_width="50dip"
            android:layout_height="50dip" />
        <TextView
            android:id="@+id/adapter_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/adapter_iv"
            android:layout_marginLeft="21dp"
            android:text="TextView" />
    </RelativeLayout>
</LinearLayout>

主Activity:

public class MainActivity extends Activity {
    private GridView gv;
    private int[] p_w_picpaths = { R.drawable.m1, R.drawable.m2, R.drawable.m3,
            R.drawable.m4, R.drawable.m5, R.drawable.m6, R.drawable.p4,
            R.drawable.m1,R.drawable.m2,R.drawable.m3,R.drawable.m4,
            R.drawable.m5
             };
    private String[] desc = { "m1", "m2", "m3", "m4", "m5", "m6", "m7", "m8",
            "m9", "m10", "m11", "m12" };
    private SimpleAdapter adapter;
    private List<Map<String, Object>> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gv = (GridView) findViewById(R.id.main_gv);
        list=new ArrayList<Map<String,Object>>();
        Map<String,Object> map;
        for(int i=0;i<p_w_picpaths.length;i++){
            map=new HashMap<String, Object>();
            map.put("p_w_picpath", p_w_picpaths[i]);
            map.put("desc", desc[i]);
            list.add(map);
        }
        adapter=new SimpleAdapter(this, list, R.layout.items,
                new String[]{"p_w_picpath","desc"},new int[]{R.id.adapter_iv,R.id.adapter_tv} );
        gv.setAdapter(adapter);
    }
}

效果图:

3、Spinner。数据会如下拉列表的形式显示。

在主布局文件中加入Spinner控件。在主Activity中实现,代码:

public class MainActivity extends Activity {
private Spinner sp;
private ArrayAdapter<String> adapter;
private String[] items={"A","B","C","D","E","F"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp=(Spinner) findViewById(R.id.spinner1);
        adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,items);
        sp.setAdapter(adapter);
    }
                                                                                        
}

执行效果:

点击后:

4、Gallery。画廊,能够把子项显示在中心锁定,水平能够滚动的列表中。

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <Gallery
        android:id="@+id/main_gl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="50dip"
        android:layout_marginTop="50dp" />
</RelativeLayout>

自定义Adapter实现:

public class MyAdapter extends BaseAdapter {
    private Context context;
    private int[] p_w_picpathid;
    public MyAdapter(Context context, int[] p_w_picpathid) {
        this.context = context;
        this.p_w_picpathid = p_w_picpathid;
    }
    @Override
    public int getCount() {
        return p_w_picpathid.length;
    }
    @Override
    public Object getItem(int position) {
        return p_w_picpathid[position];
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView iv = new ImageView(context);
        iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        iv.setBackgroundResource(p_w_picpathid[position]);
        return iv;
    }
}

主Activity:

public class MainActivity extends Activity {
private Gallery gl;
private MyAdapter adapter;
private int[] ids={R.drawable.o1,R.drawable.o2,R.drawable.o3,R.drawable.o4,
        R.drawable.o5,R.drawable.o6,R.drawable.o7};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gl=(Gallery) findViewById(R.id.main_gl);
        adapter=new MyAdapter(this, ids);
        gl.setAdapter(adapter);   
    }
}

执行效果是一组连续的能够滚动的图片。

好了,仔细阅读代码,而后实践一下,相信你必定会很快掌握的,好运。