title: How to create a Photo Gallery on Android?
body: <p>I want to create a photo gallery on Android. I'm using <code>Gallery</code> and <code>BaseAdapter</code> to create a scrollable gallery. Now I want to add an action when an image of the gallery is shown (each image's width is the same as screen width), so I need to get its index in the image array. My question is <strong>how to get the index of the image</strong> which shows on the screen? </p>
  
  <p>I want to get the index of the image as soon as it shows on the screen, not on click event.
  I've tried to get current image position in <code>getView()</code>, but the result is strange: </p>
  
  <p>I scroll to image02 but position=2 (should be 1). When I scroll back, image02's position=0 (should be 1). </p>
  
  <pre><code>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 is 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;  
      }
  </code></pre>