Reputation: 329
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;
}
}
Upvotes: 0
Views: 457
Reputation: 26034
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;
}
Upvotes: 0
Reputation: 2681
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.
Upvotes: 1
Reputation: 657
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 ...
Upvotes: 0
Reputation: 5971
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.
Upvotes: 0