Advertisement
Guest User

some example code

a guest
Feb 13th, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. public class myPage extends Activity {
  2.  
  3.  
  4.     private Gallery g =null;
  5.     private Context mContext = null;
  6.    
  7.     @Override    
  8.     public void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.cheongfun);
  11.  
  12.         if (g==null)
  13.         {
  14.             Gallery g = (Gallery) findViewById(R.id.gallery);
  15.             g.setAdapter(new ImageAdapter(this));
  16.             g.setOnItemClickListener(new OnItemClickListener() {
  17.                 public void onItemClick(AdapterView parent, View v, int position, long id) {
  18.                     Toast.makeText(myPage.this, "" + position, Toast.LENGTH_SHORT).show();
  19.                 }
  20.             });
  21.            
  22.             g.setSelection(3);
  23.  
  24.         }
  25.     }
  26.    
  27.     @Override
  28.     public void onPause()
  29.     {
  30.         super.onPause();
  31.         g = null;
  32.         finish();
  33.     }
  34.    
  35.     @Override
  36.     public void onStop()
  37.     {
  38.         super.onStop();
  39.         g = null;
  40.         finish();
  41.     }
  42.    
  43.    
  44.    
  45.  
  46.     public class ImageAdapter extends BaseAdapter {
  47.         int mGalleryItemBackground;
  48.         private Context mContext;
  49.  
  50.         private Integer[] mImageIds = {
  51.                 R.drawable.cheongfun1,
  52.                 R.drawable.cheongfun2,
  53.                 R.drawable.cheongfun3,
  54.                 R.drawable.cheongfun4,
  55.                 R.drawable.cheongfun5,
  56.                 R.drawable.cheongfun6,
  57.                 R.drawable.cheongfun7
  58.         };
  59.  
  60.         public ImageAdapter(Context c) {
  61.             mContext = c;
  62.             TypedArray a = obtainStyledAttributes(R.styleable.DefaultGallery);
  63.             mGalleryItemBackground = a.getResourceId(
  64.                     R.styleable.DefaultGallery_android_galleryItemBackground, 0);
  65.             a.recycle();
  66.         }
  67.  
  68.         public int getCount() {
  69.             return mImageIds.length;
  70.         }
  71.  
  72.         public Object getItem(int position) {
  73.             return position;
  74.         }
  75.  
  76.         public long getItemId(int position) {
  77.             return position;
  78.         }
  79.  
  80.         public View getView(int position, View convertView, ViewGroup parent) {
  81.             ImageView i = new ImageView(mContext);
  82.  
  83.             i.setImageResource(mImageIds[position]);
  84.             i.setLayoutParams(new Gallery.LayoutParams(300, 200));
  85.             i.setScaleType(ImageView.ScaleType.FIT_XY);
  86.             i.setBackgroundResource(mGalleryItemBackground);
  87.  
  88.             return i;
  89.         }
  90.     }    
  91.    
  92.     public void onResume() {
  93.         super.onResume();        
  94.         setContentView(R.layout.cheongfun);
  95.         if (g==null)
  96.         {
  97.             Gallery g = (Gallery) findViewById(R.id.gallery);
  98.             g.setAdapter(new ImageAdapter(this));
  99.             g.setOnItemClickListener(new OnItemClickListener() {
  100.                 public void onItemClick(AdapterView parent, View v, int position, long id) {
  101.                     Toast.makeText(myPage.this, "" + position, Toast.LENGTH_SHORT).show();
  102.                 }
  103.             });
  104.            
  105.             g.setSelection(3);
  106.  
  107.         }
  108.        
  109.        
  110.     }    
  111.     public void onConfigurationChanged(Configuration newConfig) {
  112.         super.onConfigurationChanged(newConfig);
  113.         setContentView(R.layout.cheongfun);
  114.         if (g==null)
  115.         {
  116.             Gallery g = (Gallery) findViewById(R.id.gallery);
  117.             g.setAdapter(new ImageAdapter(this));
  118.             g.setOnItemClickListener(new OnItemClickListener() {
  119.                 public void onItemClick(AdapterView parent, View v, int position, long id) {
  120.                     Toast.makeText(myPage.this, "" + position, Toast.LENGTH_SHORT).show();
  121.                 }
  122.             });
  123.            
  124.             g.setSelection(3);
  125.  
  126.         }
  127.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement