Custom adapter on both GridView and Gallery
It's a example using the same custom adapter on both GridView and Gallery. It can be noticed that adapter used on both GridView and Gallery are basically the same.
/res/layout/gridlayout.xml
layout file , main.xml
main java code
/res/layout/gridlayout.xml
1
2
3
4
5
6
7
8
9
10
11
12
| <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:orientation = "vertical" > < ImageView android:id = "@+id/image" android:layout_width = "70dp" android:layout_height = "wrap_content" android:layout_gravity = "center" /> </ LinearLayout > |
layout file , 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" ?> android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/hello" /> < Gallery android:id = "@+id/gallery" android:layout_width = "fill_parent" android:layout_height = "50dp" /> < GridView android:id = "@+id/grid" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:numColumns = "2" android:columnWidth = "100px" android:stretchMode = "columnWidth" android:gravity = "center" /> </ LinearLayout > |
main java code
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
83
84
85
86
87
88
89
90
91
92
93
| package com.AndroidGridView; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.GridView; import android.widget.ImageView; public class AndroidGridViewActivity extends Activity { public class MyAdapter extends BaseAdapter { final int NumberOfItem = 30 ; private Bitmap[] bitmap = new Bitmap[NumberOfItem]; private Context context; private LayoutInflater layoutInflater; MyAdapter(Context c){ context = c; layoutInflater = LayoutInflater.from(context); //init dummy bitmap, //using R.drawable.icon for all items for ( int i = 0 ; i < NumberOfItem; i++){ bitmap[i] = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon); } } @Override public int getCount() { // TODO Auto-generated method stub return bitmap.length; } @Override public Object getItem( int position) { // TODO Auto-generated method stub return bitmap[position]; } @Override public long getItemId( int position) { // TODO Auto-generated method stub return position; } @Override public View getView( int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View grid; if (convertView== null ){ grid = new View(context); grid = layoutInflater.inflate(R.layout.gridlayout, null ); } else { grid = (View)convertView; } ImageView imageView = (ImageView)grid.findViewById(R.id.image); imageView.setImageBitmap(bitmap[position]); return grid; } } GridView gridView; Gallery gallery; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); gridView = (GridView)findViewById(R.id.grid); gallery = (Gallery)findViewById(R.id.gallery); MyAdapter adapter = new MyAdapter( this ); gridView.setAdapter(adapter); gallery.setAdapter(adapter); } } |
No comments:
Post a Comment