Implement a photo bar using android.widget.Gallery, with custom BaseAdapter, and custom object.
In this example, I will implement a photo bar using android.widget.Gallery.
We will implement MyAdapter extends BaseAdapter, it hold a ArrayList of our custom object ImageItem. In the constructor of MyAdapter, an empty ArrayList of ImageItem will be new. And a method addImageItem is implemented to add new items into the ArrayList.
insertDummyImageItem(3) is a dummy method to add new items into the ArrayList of MyAdapter. As a example, all items have the same bitmap - R.drawable.icon. After items inserted, we have to call notifyDataSetChanged() of the MyAdapter to update the photo bar.
In such a approach, we can add new item on runtime. We will do more on coming post "Update BaseAdapter in backgrond Thread, to insert Gallery items in run-time.".
We will implement MyAdapter extends BaseAdapter, it hold a ArrayList of our custom object ImageItem. In the constructor of MyAdapter, an empty ArrayList of ImageItem will be new. And a method addImageItem is implemented to add new items into the ArrayList.
insertDummyImageItem(3) is a dummy method to add new items into the ArrayList of MyAdapter. As a example, all items have the same bitmap - R.drawable.icon. After items inserted, we have to call notifyDataSetChanged() of the MyAdapter to update the photo bar.
In such a approach, we can add new item on runtime. We will do more on coming post "Update BaseAdapter in backgrond Thread, to insert Gallery items in run-time.".
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
| package com.AndroidPhotoBar; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class AndroidPhotoBarActivity extends Activity { ArrayList<ImageItem> arrayImageItem; MyAdapter myAdapter; public class ImageItem { Bitmap bitmapImage; ImageItem(){ //To simplify, we use a default image here bitmapImage = BitmapFactory.decodeResource( AndroidPhotoBarActivity. this .getResources(), R.drawable.icon); } public Bitmap getImage(){ return bitmapImage; } } Gallery myPhotoBar; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); myPhotoBar = (Gallery)findViewById(R.id.photobar); myAdapter = new MyAdapter( this ); //init myAdapter without ImageItem //insert dummy ImageItem into myAdapter insertDummyImageItem( 2 ); myPhotoBar.setAdapter(myAdapter); //insert dummy ImageItem into myAdapter in Run time insertDummyImageItem( 3 ); myAdapter.notifyDataSetChanged(); } private void insertDummyImageItem( int cnt){ //Insert dummy ImageItem into myAdapter for ( int i = 0 ; i < cnt; i++){ myAdapter.addImageItem( new ImageItem()); } } public class MyAdapter extends BaseAdapter { Context context; ArrayList<ImageItem> _arrayImageItem; MyAdapter(Context c){ context = c; _arrayImageItem = new ArrayList<ImageItem>(); } public void addImageItem(ImageItem item){ _arrayImageItem.add(item); } public int getCount() { // TODO Auto-generated method stub return _arrayImageItem.size(); } public Object getItem( int position) { // TODO Auto-generated method stub return _arrayImageItem.get(position); } public long getItemId( int position) { // TODO Auto-generated method stub return position; } public View getView( int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView imageView; imageView = new ImageView(context); imageView.setLayoutParams( new Gallery.LayoutParams( 150 , 150 )); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setImageBitmap(_arrayImageItem.get(position).getImage()); return imageView; } } }
akm www.cdacians.com |
No comments:
Post a Comment