Friday, January 4, 2013

[Android] ListView自定圖文


先創出主要Layout(包含一個ListView),裡面的清單項目由另一個xml去部屬,來達到圖文的清單。而清單的容器使用SimpleAdapter 來自訂自己喜歡的樣式,點選後會顯示 ID 還有內容字串陣列

ListView2.java
01private SimpleAdapter adapter;
02    private ListView listView2;
03 
04    @Override
05    public void onCreate(Bundle savedInstanceState) {
06        super.onCreate(savedInstanceState);
07        setContentView(R.layout.listview2);
08 
09        listView2 = (ListView) findViewById(R.id.listView2);
10 
11        // 清單面版
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() {
18            @Override
19            public void onItemClick(AdapterView arg0, View arg1, int arg2,
20                    long arg3) {
21                // TODO Auto-generated method stub
22                ListView listView = (ListView) arg0;
23                Toast.makeText(
24                        ListView2.this,
25                        "ID:" + arg3 +
26                        "   選單文字:"+ listView.getItemAtPosition(arg2).toString(),
27                        Toast.LENGTH_LONG).show();
28            }
29        });
30    }
01/**
02 * 設定清單資料
03 * */
04private List getData() {     
05    List""> list = new ArrayList"">();
06     
07    Map""> map = new HashMap"">();
08    map.put("title""G1");
09    map.put("info""紅豆");
10    map.put("img", R.drawable.icon);
11    list.add(map);
12 
13    map = new HashMap"">();
14    map.put("title""G2");
15    map.put("info""綠豆");
16    map.put("img", R.drawable.icon);
17    list.add(map);
18 
19    map = new HashMap"">();
20    map.put("title""G3");
21    map.put("info""土豆");
22    map.put("img", R.drawable.icon);
23    list.add(map);
24 
25    map = new HashMap"">();
26    map.put("title""G4");
27    map.put("info""毛豆");
28    map.put("img", R.drawable.icon);
29    list.add(map);
30     
31    return list;
32}
/map


主要的版面 裡面只有一個 ListView物件 listview2.xml
1<span style="font-family:trebuchet ms,helvetica,sans-serif;">xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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>
7</LinearLayout>
8</span>

在 ListView 裡面自定顯示排版,在裡面放置了兩個TextView、一個 ImageView
view2content.xml
01<span style="font-family:trebuchet ms,helvetica,sans-serif;">xml version="1.0" encoding="utf-8"?>
02<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03    android:layout_width="match_parent" android:layout_height="match_parent"
04    android:orientation="horizontal">
05    <ImageView
06        android:src="@drawable/icon"   
07        android:layout_height="wrap_content"
08        android:layout_width="wrap_content"
09        android:id="@+id/img"></ImageView>
10    <LinearLayout
11    android:id="@+id/linearLayout1"
12    android:layout_width="match_parent"
13    android:layout_height="wrap_content"
14    android:orientation="vertical">
15        <TextView
16            android:text="TextView"
17            android:layout_width="wrap_content"
18            android:layout_height="wrap_content"
19            android:id="@+id/title"></TextView>
20        <TextView
21            android:layout_height="wrap_content"
22            android:layout_width="wrap_content"
23            android:text="TextView"
24            android:id="@+id/info"></TextView>
25    </LinearLayout>
26</LinearLayout>
27</span>

No comments:

Post a Comment