How to create a Photo Gallery on Android? -
i want create photo gallery on android. i'm using gallery
, baseadapter
create scrollable gallery. want add action when image of gallery shown (each image's width same screen width), need index in image array. question how index of image shows on screen?
i want index of image shows on screen, not on click event. i've tried current image position in getview()
, result strange:
i scroll image02 position=2 (should 1). when scroll back, image02's position=0 (should 1).
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); gallery g = (gallery) this.findviewbyid(r.id.gallery); g.setadapter(new imageadapter(this)); } public class imageadapter extends baseadapter { private context mcontext; private integer[] mimageids = { //each image's size 320x60 r.drawable.btm01, r.drawable.btm02, r.drawable.btm03 }; public imageadapter(context c){ this.mcontext = c; } @override public int getcount() { // todo auto-generated method stub return mimageids.length; } @override public object getitem(int position) { // todo auto-generated method stub return null; } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub imageview imageview = new imageview(mcontext); imageview.setimageresource(mimageids[position]); imageview.setlayoutparams(new gallery.layoutparams(320,60)); return imageview; }
check this example.
// reference gallery view gallery g = (gallery) this.findviewbyid(r.id.gallery); // set adapter our custom adapter (below) g.setadapter(new imageadapter(this)); // set item click listener, , toast clicked position g.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview parent, view v, int position, long id) { toast.maketext(gallery1.this, "" + position, toast.length_short).show(); } });
Comments
Post a Comment