一、简单ListView控件
通过数组适配器把数组集合中每个元素信息用android.R.layout.simple_list_item_1方式放入ListView
点每一个item时,反应通过对listView对象设置setOnItemClickListener方法实现。
UI2Activity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_listview);
ListView listView = (ListView)findViewById(R.id.listview_lv);
final ArrayList arrayList = new ArrayList();
arrayList.add("item1");
arrayList.add("item2");
arrayList.add("item3");
ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(UI2Activity.this, ""+arrayList.get(position), Toast.LENGTH_SHORT).show();
}
});
}
界面布局layout_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:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview_lv">
</ListView>
</LinearLayout>
二、复杂ListView控件
用哈希网(键值对)的方式把信息建立每一个元素,再放入线性表中,通过简单适配器把线性表集合中每个元素信息用自定义的界面xml R.layout.listitemlayout,并对应指明键与自定义界面的控件名称放入ListView。
UI2Activity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_listview);
ListView listView = (ListView)findViewById(R.id.listview_lv);
ArrayList arraylist = new ArrayList();
HashMap hashMap;
for (int i =0; i<20;i++) {
hashMap = new HashMap();
hashMap.put("product", "product1");
hashMap.put("category", "category1");
hashMap.put("img", android.R.drawable.ic_delete);
arraylist.add(hashMap);
hashMap = new HashMap();
hashMap.put("product", "product2");
hashMap.put("category", "category2");
hashMap.put("img", android.R.drawable.ic_dialog_alert);
arraylist.add(hashMap);
hashMap = new HashMap();
hashMap.put("product", "product3");
hashMap.put("category", "category3");
hashMap.put("img", android.R.drawable.ic_btn_speak_now);
arraylist.add(hashMap);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this,arraylist,R.layout.listitemlayout,new String[]{"product","category","img"},new int[]{R.id.item_product,R.id.item_category,R.id.item_img});
listView.setAdapter(simpleAdapter);
}
每一个Item布局
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_img"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_product"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_category"/>
</LinearLayout>
</LinearLayout>
界面布局layout_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:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview_lv">
</ListView>
</LinearLayout>