Gallery image click event,

Source: Internet
Author: User

Gallery image click event,

Package com. example. gallery; import com. example. gallery. r; import android. OS. bundle; import android. app. activity; import android. content. context; import android. view. menu; import android. view. view; import android. view. viewGroup; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. baseAdapter; import android. widget. gallery; import android. widget. imageV Iew; import android. widget. toast; public class MainActivity extends Activity {/** Called when the activity is first created. */private Gallery gallery; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); gallery = (Gallery) findViewById (R. id. gallery); // sets the image adapter gallery. setAdapter (new ImageAdapter (this); // sets the listener gal Lery. setOnItemClickListener (new OnItemClickListener () {public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {Toast. makeText (MainActivity. this, "click the" + (arg2 + 1) + "picture", Toast. LENGTH_LONG ). show () ;}}) ;}} class ImageAdapter extends BaseAdapter {private Context context Context; // image source array private Integer [] imageInteger = {R. drawable. item1, R. drawable. item2, R. drawable. item3, R. drawable. item4, R. drawable. item5, R. drawable. item6, R. drawable. item7, R. drawable. item8, R. drawable. item9, R. drawable. item10, R. drawable. item11, R. drawable. item12, R. drawable. item13, R. drawable. item14, R. drawable. item15}; public ImageAdapter (Context c) {context = c ;}@ Override public int getCount () {return imageInteger. length ;}@ Override public Object getItem (int position) {// TODO Auto-generated method stub return position ;}@ Override public long getItemId (int position) {// TODO Auto-generated method stub return position;} @ Override public View getView (int position, View convertView, ViewGroup parent) {ImageView imageView = new ImageView (context); imageView. setImageResource (imageInteger [position]); imageView. setBackgroundColor (0xFF000000); imageView. setScaleType (ImageView. scaleType. FIT_CENTER); imageView. setLayoutParams (new Gallery. layoutParams (136, 88); return imageView ;}}

Result execution diagram:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.