Gallery:
The image filling method is similar to that of the spinner. The setadapter () method is also used. However, the custom adapter is used, and the adapter must inherit from the baseadapter.
Gallery definition and data filling and listening events:
Gallery G = (Gallery) findviewbyid (R. id. mygallery); <br/>/* add an imageadapter and set it to the gallery object */<br/> G. setadapter (New imageadapter (this); </P> <p>/* set an itemclicklistener and toast the position of the clicked image */<br/> G. setonitemclicklistener (New onitemclicklistener () <br/>{< br/> Public void onitemclick <br/> (adapterview <?> Parent, view V, int position, long ID) <br/>{< br/> toast. maketext <br/> (ex04_10.this, getstring (R. string. my_gallery_text_pre) <br/> + Position + getstring (R. string. my_gallery_text_post), <br/> toast. length_short ). show (); <br/>}< br/> });
The imageadapter class inherited from baseadapter can be implemented as an internal class:
Public class imageadapter extends baseadapter <br/> {<br/>/* declare variable */<br/> int mgalleryitembackground; <br/> private context mcontext; </P> <p>/* imageadapter constructor */<br/> Public imageadapter (context c) <br/>{< br/> mcontext = C; </P> <p>/* used in res/values/attrs. <declare-styleable> defines the gallery attribute of <br/> * in XML. */<br/> typedarray A = obtainstyledattributes (R. styleable. gallery); </P> <p>/* obtain the index ID of the gallery attribute */<br/> mgalleryitembackground =. getresourceid <br/> (R. styleable. gallery_android_galleryitembackground, 0); </P> <p>/* enables repeated use of the styleable attribute of the object */<br/>. recycle (); <br/>}</P> <p>/* overwrite the getcount method, returns the number of images */<br/> Public int getcount () <br/>{< br/> return myimageids. length; <br/>}</P> <p>/* overwrite the getitemid method, returns the array ID of the image */<br/> Public object getitem (INT position) <br/>{< br/> return position; <br/>}< br/> Public long getitemid (INT position) <br/>{< br/> return position; <br/>}</P> <p>/* overwrite the getview method. A view object */<br/> Public View getview <br/> (INT position, view convertview, viewgroup parent) <br/>{< br/>/* generate an imageview object */<br/> imageview I = new imageview (mcontext ); <br/>/* set the image to the imageview object */<br/> I. setimageresource (myimageids [position]); <br/>/* reset the width and height of the image */<br/> I. setscaletype (imageview. scaletype. fit_xy); <br/>/* reset the width and height of Layout */<br/> I. setlayoutparams (new gallery. layoutparams (136, 88); <br/>/* set the gallery background image */<br/> I. setbackgroundresource (mgalleryitembackground); <br/>/* return imageview object */<br/> return I; <br/>}</P> <p>/* construct an integer array and obtain the ID of the pre-loaded drawable image */<br/> private integer [] myimageids = <br />{< br/> r. drawable. photo1, <br/> r. drawable. photo2, <br/> r. drawable. photo3, <br/> r. drawable. photo4, <br/> r. drawable. photo5, <br/> r. drawable. photo6, <br/>}; <br/>}
R. styleable. gallery_android_galleryitembackground is defined in the external attrs. xml file:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <declare-styleable name = "Gallery"> <br/> <ATTR name = "Android: galleryitembackground "/> <br/> </declare-styleable> <br/> </resources>