Gallery is an official Android View Container class, inherited from the Absspinner class, to achieve page sliding effects.
From the above inheritance relationship, we can see that the Absspinner class inherits from Adapterview, so we can customize the implementation adapter to populate the Gallery container's data.
This example fills the picture data of the gallery container by implementing a adapter of its own, looking at the effect first:
Activity
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.Gallery;
Import Android.widget.Toast;
public class Galleryactivity extends activity {@Override public void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Gallery Gallery = (Gallery) Findviewbyid (r.id.gallery); Gallery.setadapter (This) (new Imageadapter); Gallery Add imageadapter Picture resource Gallery.setonitemclicklistener (listener); Gallery Settings Click on the picture resource event} Adapterview.onitemclicklistener listener = new ADAPTERVIEW.ONITEMC Licklistener () {@Override public void Onitemclick (adapterview<?> parent, view view, int posit ion, long ID) {Toast.maketext (galleryactivity.this, "picture" + (Position + 1), Toast.length_short). Show ();
}
}; }