Android general and cyclic Gallery

Source: Internet
Author: User

Android general and cyclic Gallery

:

Code

Main. java

Package com. example. gallerydemo; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. menu; import android. view. menuItem; 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. imageView; import android. widget. textView; import android. widget. toast; import android. widget. imageView. scaleType; public class Main extends Activity {private Gallery gallery = null; private Gallery galleryLoop = null; private TextView tvNum = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); tvNum = (TextView) findViewById (R. id. num); gallery = (Gallery) findViewById (R. id. gallery); gallery. setAdapter (new ImageAdapter (this); gallery. setSpacing (5); gallery. setOnItemClickListener (new OnItemClickListener () {public void onItemClick (AdapterView
 Arg0, View arg1, int arg2, long arg3) {int num = arg2 + 1; // Toast. makeText (Main. this, "click" + num, Toast. LENGTH_SHORT )//. show (); tvNum. setText ("General Gallery: click" + num) ;}}); galleryLoop = (Gallery) findViewById (R. id. galleryLoop); galleryLoop. setAdapter (new LoopImageAdapter (this); galleryLoop. setSpacing (10); galleryLoop. setOnItemClickListener (new OnItemClickListener () {public void onItemClick (AdapterView
 Arg0, View arg1, int arg2, long arg3) {int num = (arg2 + 1) % 9; if (num = 0) {num = 9;} // Toast. makeText (Main. this, "click" + num, Toast. LENGTH_SHORT )//. show (); tvNum. setText ("loop Gallery: click" + num) ;}}) ;}// Adapterclass ImageAdapter extends BaseAdapter {private Context context Context; public ImageAdapter (context Context) of General Gallery) {this. context = context;} private Integer [] imageInteger = {R. drawable. pic_1, R. drawable. pic_2, R. drawable. pic_3, R. drawable. pic_4, R. drawable. pic_5, R. drawable. pic_6, R. drawable. pic_7, R. drawable. pic_8, R. drawable. pic_9}; public Object getItem (int position) {return position;} public long getItemId (int position) {return position;} public int getCount () {return imageInteger. length;} public View getView (int position, View convertView, ViewGroup parent) {ImageView imageView = new ImageView (context); imageView. setImageResource (imageInteger [position]); imageView. setScaleType (ImageView. scaleType. FIT_XY); imageView. setLayoutParams (new Gallery. layoutParams (100,100); return imageView ;}// the Adapterclass LoopImageAdapter extends BaseAdapter {private Context context Context; public LoopImageAdapter (context Context) {this. context = context;} private Integer [] imageInteger = {R. drawable. pic_1, R. drawable. pic_2, R. drawable. pic_3, R. drawable. pic_4, R. drawable. pic_5, R. drawable. pic_6, R. drawable. pic_7, R. drawable. pic_8, R. drawable. pic_9}; public Object getItem (int position) {return position;} public long getItemId (int position) {return position;} public int getCount () {return Integer. MAX_VALUE;} public View getView (int position, View convertView, ViewGroup parent) {ImageView imageView = new ImageView (context); imageView. setImageResource (imageInteger [position % imageInteger. length]); imageView. setScaleType (ImageView. scaleType. FIT_XY); imageView. setLayoutParams (new Gallery. layoutParams (100,100); return imageView; }}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml.int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}

Main. xml

     
      
      
      
      
  
 


Reprinted please indicate the source: Zhou mu Shui CSDN blog http://blog.csdn.net/zhoumushui

My GitHub: Zhou mu Shui's GitHub https://github.com/zhoumushui


Related Article

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.