先創出主要Layout(包含一個ListView),裡面的清單項目由另一個xml去部屬,來達到圖文的清單。而清單的容器使用SimpleAdapter 來自訂自己喜歡的樣式,點選後會顯示 ID 還有內容字串陣列
ListView2.java
01 | private SimpleAdapter adapter; |
02 | private ListView listView2; |
05 | public void onCreate(Bundle savedInstanceState) { |
06 | super.onCreate(savedInstanceState); |
07 | setContentView(R.layout.listview2); |
09 | listView2 = (ListView) findViewById(R.id.listView2); |
12 | adapter = new SimpleAdapter(this, getData(), |
13 | R.layout.view2content, |
14 | new String[] { "title", "info", "img" }, |
15 | new int[] { R.id.title, R.id.info, R.id.img }); |
16 | listView2.setAdapter(adapter); |
17 | listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
19 | public void onItemClick(AdapterView arg0, View arg1, int arg2, |
22 | ListView listView = (ListView) arg0; |
26 | " 選單文字:"+ listView.getItemAtPosition(arg2).toString(), |
27 | Toast.LENGTH_LONG).show(); |
04 | private List getData() { |
05 | List |
07 | Map""> map = new HashMap"">(); |
08 | map.put("title", "G1"); |
09 | map.put("info", "紅豆"); |
10 | map.put("img", R.drawable.icon); |
13 | map = new HashMap"">(); |
14 | map.put("title", "G2"); |
15 | map.put("info", "綠豆"); |
16 | map.put("img", R.drawable.icon); |
19 | map = new HashMap"">(); |
20 | map.put("title", "G3"); |
21 | map.put("info", "土豆"); |
22 | map.put("img", R.drawable.icon); |
25 | map = new HashMap"">(); |
26 | map.put("title", "G4"); |
27 | map.put("info", "毛豆"); |
28 | map.put("img", R.drawable.icon); |
/map
主要的版面 裡面只有一個 ListView物件 listview2.xml
1 | <span style="font-family:trebuchet ms,helvetica,sans-serif;">xml version="1.0" encoding="utf-8"?> |
3 | android:layout_width="match_parent" android:layout_height="match_parent" |
4 | android:orientation="vertical"> |
5 | <ListView android:layout_height="wrap_content" |
6 | android:layout_width="match_parent" android:id="@+id/listView2"></ListView> |
在 ListView 裡面自定顯示排版,在裡面放置了兩個TextView、一個 ImageView
view2content.xml
01 | <span style="font-family:trebuchet ms,helvetica,sans-serif;">xml version="1.0" encoding="utf-8"?> |
03 | android:layout_width="match_parent" android:layout_height="match_parent" |
04 | android:orientation="horizontal"> |
06 | android:src="@drawable/icon" |
07 | android:layout_height="wrap_content" |
08 | android:layout_width="wrap_content" |
09 | android:id="@+id/img"></ImageView> |
11 | android:id="@+id/linearLayout1" |
12 | android:layout_width="match_parent" |
13 | android:layout_height="wrap_content" |
14 | android:orientation="vertical"> |
16 | android:text="TextView" |
17 | android:layout_width="wrap_content" |
18 | android:layout_height="wrap_content" |
19 | android:id="@+id/title"></TextView> |
21 | android:layout_height="wrap_content" |
22 | android:layout_width="wrap_content" |
23 | android:text="TextView" |
24 | android:id="@+id/info"></TextView> |
No comments:
Post a Comment