androidimage-gallerygalleryview

Which line of code is telling Android to display all images in GalleryView?


The following code is a typical way to use GalleryView in Android. However, I don't understand which line of code actually display all images 1-8.

I understand that all images 1-8 are stored in the array ImageIds. However the following code inside getView{...} after @Override only display one particular image (ImageIds[position]) in which position is the one you select.

i.setImageResource(ImageIds[position]); 

Therefore, which line of code is telling Android to display all ImageIds images?

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent" 
     android:orientation="vertical">


     <Gallery
         android:id="@+id/gallery1"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" />

      <ImageView
         android:id="@+id/imageView1"
         android:layout_marginTop="100dp"
         android:layout_width="250dp"
         android:layout_gravity="center_horizontal"
         android:layout_height="250dp"
         android:src="@drawable/image1" />
</LinearLayout>

MainActivity

public class MainActivity extends Activity 
{

      ImageView selectedImage;  
      private Integer[] ImageIds = {
                 R.drawable.image1,
                 R.drawable.image2,
                 R.drawable.image3,
                 R.drawable.image4,
                 R.drawable.image5,
                 R.drawable.image6,
                 R.drawable.image7,
                 R.drawable.image8
         };
     @Override
     public void onCreate(Bundle savedInstanceState) 
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

              Gallery gallery = (Gallery) findViewById(R.id.gallery1);
         selectedImage=(ImageView)findViewById(R.id.imageView1);
         gallery.setSpacing(1);
         gallery.setAdapter(new GalleryImageAdapter(this));

          // clicklistener for Gallery
         gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(MainActivity.this, "Your selected position = " + position, Toast.LENGTH_SHORT).show();
                // show the selected Image
                selectedImage.setImageResource(ImageIds[position]);
             }
         });
     }

 }

GalleryImageAdapter 

 public class GalleryImageAdapter extends BaseAdapter 
 {
     private Context mContext;

     private Integer[] ImageIds = {
             R.drawable.image1,
             R.drawable.image2,
             R.drawable.image3,
             R.drawable.image4,
             R.drawable.image5,
             R.drawable.image6,
             R.drawable.image7,
             R.drawable.image8
     };

     public GalleryImageAdapter(Context context) 
     {
         mContext = context;
     }

     public int getCount() {
         return ImageIds.length;
     }

     public Object getItem(int position) {
         return position;
     }

     public long getItemId(int position) {
         return position;
     }


     // Override this method according to your need
     public View getView(int position, View view, ViewGroup viewGroup) 
     {
         // TODO Auto-generated method stub
         ImageView i = new ImageView(mContext);

         i.setImageResource(ImageIds[position]);
         i.setLayoutParams(new Gallery.LayoutParams(200, 200));

         i.setScaleType(ImageView.ScaleType.FIT_XY);

         return i;
     }
 }

Solution

  • The setAdapter method of the Gallery calls all the callback methods of the adapter that u've supplied in it as the parameter.

    That is,

    gallery.setAdapter(new GalleryImageAdapter(this));
    

    calls your GalleryImageAdapter, which internally calls its getView() the number of times that you've given in the getCount method.

    public int getCount() {
        return ImageIds.length;
    }
    

    will call the getView() method ImageIds.length number of times.