Gallery is a horizontal list selection box that allows you to drag to view the previous and next list options.
The following are additional properties of the control Gallery:
To use a Gallery, you only need to set the Adapter that fills its content. From the perspective of the Adapter system (you can look at the Adapter in Android), it is obvious that using BaseAdapter is the best choice. Of course SimpleAdapter is also acceptable. However, it is not clear and powerful to implement BaseAdapter. So here's Best Practice, I personally think it is BaseAdapter.
Gallery is generally used to display images. Of course, it is often used to display custom la S and pseudo 3D effects. The following is a simple example to explain the usage of Gallery.
1. First, define the adapter that fills the Gallery. Here, select inherit BaseAdapter to customize your own adapter.
[Html]
Public class GalleryAdapter extends BaseAdapter {
Private Context context;
Private Integer [] imagesId; // the image to be displayed
Public GalleryAdapter (Context context ){
This. context = context;
ImagesId = new Integer [] {R. drawable. a, R. drawable. B, R. drawable. c, R. drawable. d };
}
// Returns the total number of images to be displayed.
Public int getCount (){
Return imagesId. length;
}
// Obtain the dataset at the specified position in the relevant data item. Here we can specify the Bitmap for this location
Public Object getItem (int position ){
Bitmap bitmap = BitmapFactory. decodeResource (context. getResources (), imagesId [position]);
Return bitmap;
}
// Return the id of the item at the relevant position. Here, return the same ID as position.
Public long getItemId (int position ){
Return position;
}
/**
* Get a View that displays the data at the specified position in the data set.
* You can either create a View manually or inflate it from an XML layout file.
* To obtain a view that displays the specified data at the specified position, you can manually create one or load one from the XML layout file.
* Parameter: position
* Parameter: If convertView is available, the old view can be reused. However, before using it, check whether it is null.
* Parameter: the parent view www.2cto.com to be attached to the parent view.
*/
Public View getView (int position, View convertView, ViewGroup parent ){
ImageView imageView = new ImageView (context );
Bitmap bitmap = BitmapFactory. decodeResource (context. getResources (), imagesId [position]);
ImageView. setImageBitmap (bitmap );
Return imageView;
}
}
2. select our Gallery. The system's Gallery is used here. Of course, you can also customize your own Gallery for some special effects.
3. Define our Gallery in the layout file and then use
[Html]
<Gallery
Android: id = "@ + id/gallery"
Android: spacing = "100dp"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
[Html]
Public class ImageScanActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Gallery gallery = (Gallery) findViewById (R. id. gallery );
GalleryAdapter adapter = new GalleryAdapter (this );
Gallery. setAdapter (adapter );
}
}
The above is the use of the simplest Gallery, of course, there are many shortcomings, such as sliding when the card is not smooth. Or a few pictures are taken. These problems are solved in the future.
From LonelyRoamer's column