本文实例分析了Android中GridView和ArrayAdapter用法。分享给大家供大家参考,具体如下:
GridView是一个表格化的二维排版的View,当GridView的文字放不下时会出现scrolling的效果,GridView中的元素命名为Item,要将Item放入GridView,需要ArrayAdapter对象。
例子如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.GridView; import android.widget.TextView; public class A06Activity extends Activity { private TextView tv; //两个按钮是动态放入GridView的开关,b01是设置GridView为两列格式,并放入4 //个item,b02设置为三列格式,并放入9个Item private Button b01,b02; // private GridView gv; private String[] s1,s2; private ArrayAdapter<String> aa; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); tv=(TextView)findViewById(R.id.tv); b01=(Button)findViewById(R.id.button01); b02=(Button)findViewById(R.id.button02); gv=(GridView)findViewById(R.id.gv); s1= new String[]{ getResources().getString(R.string.str_list01), getResources().getString(R.string.str_list02), getResources().getString(R.string.str_list03), getResources().getString(R.string.str_list04) }; s2= new String[]{ getResources().getString(R.string.str_list01), getResources().getString(R.string.str_list02), getResources().getString(R.string.str_list03), getResources().getString(R.string.str_list04), getResources().getString(R.string.str_list01), getResources().getString(R.string.str_list02), getResources().getString(R.string.str_list03), getResources().getString(R.string.str_list04), getResources().getString(R.string.str_list04) }; b01.setOnClickListener( new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub gv.setNumColumns( 2 ); aa= new ArrayAdapter<String>(A06Activity. this ,R.layout.list_item_l_small,s1); gv.setAdapter(aa); gv.setSelection( 2 ); gv.refreshDrawableState(); } }); b02.setOnClickListener( new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub gv.setNumColumns( 2 ); aa= new ArrayAdapter<String>(A06Activity. this ,R.layout.list_item_l_small,s2); gv.setAdapter(aa); } }); //利用GridView的OnClickListener()方法用来捕捉用户单击的是哪个item gv.setOnItemClickListener( new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // 判断Adapter中元素的个数,判断被单击的是第几个元素的名称 switch (aa.getCount()){ case 4 : tv.setText(s1[arg2]); break ; case 9 : tv.setText(s2[arg2]); break ; } } }); } } |
res/layout/main.xml如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:orientation = "vertical" > < TextView android:id = "@+id/tv" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/hello" /> < Button android:id = "@+id/button01" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> < Button android:id = "@+id/button02" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> < GridView android:id = "@+id/gv" android:layout_width = "fill_parent" android:layout_height = "wrap_content" ></ GridView > </ LinearLayout > |
res/layout/list_item_l_small的作用是配置ArrayAdapter的第二个参数textViewResourceId,参数必须为TextView进行配置。若需要更改放入item的文字模式,可以通过这个来设置文字的大小、模式的属性设置,而一旦放入ArrayAdapter,这个配置会立即生效。也可以设置许多的layout xml,提供给不同的GridView使用,让每一个GridView都有不同的风貌。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<? xml version = "1.0" encoding = "utf-8" ?> < TextView xmlns:android = "http://schemas.android.com/apk/res/android" android:id = "@+id/myCheckedTextView1" android:layout_width = "match_parent" android:layout_height = "match_parent" android:textColor = "@drawable/blue" android:textSize = "12dp" android:textAppearance = "@android:attr/textAppearanceLarge" android:gravity = "center_vertical" android:paddingLeft = "6dip" android:minHeight = "@android:attr/listPreferredItemHeight" /> |
希望本文所述对大家Android程序设计有所帮助。