Question

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;
     }
 }
Was it helpful?

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.

OTHER TIPS

The method

public View getView(int position, View view, ViewGroup viewGroup) 

in your adapter sets the images from your image-array to your adapter. As you call

ImageView i = new ImageView(mContext);
i.setImageResource(ImageIds[position]);

and

gallery.setAdapter(new GalleryImageAdapter(this));

sets your adapter to your gallery.

When android needs to show a particular page, It will call getView method to get UI for that page. As you move through all pages, getView will be called for corresponding positions i.e. 1, 2, 3 ...

If you have marked in BaseAdapter's unimplemented methods, there is getCount method. As we are doing code in for loop, same functionality is also here.

getCount will return length of array and it will execute from first element of your array to length you have provided in your getCount method which is following.

public int getCount() {
     return ImageIds.length;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top