介绍过Adapter之后,让我们来了解一下常用的AdapterView的使用吧。ListView 与GridView、Spinner与Gallery。他们是两组分别来自AbsListView和AbsSpinner的子类。所以会有一定的相似性。他们都需要Adapter才能将数据显示在控件上。下面会结合具体实例讲解每个控件的用法。
一、ListView。将内容以列表的形式显示,是最常用的AdapterView控件。
主布局文件:
<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的资源文件。
<?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:
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);
}
}
运行效果:
二、GridView。
在主布局文件中加入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>
效果:
定义布局文件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);
}
}
效果图:
三、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);
}
}
执行效果:
点击后:
四、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);
}
}
执行效果是一组连续的可以滚动的图片。
好了,仔细阅读代码,然后实践一下,相信你一定会很快掌握的,好运。
转载于:https://blog.51cto.com/zixia/1281682