pager.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id) {
public void restoreState(Parcelable state, ClassLoader loader) {
}
public Parcelable saveState() {
return null;
}
public void startUpdate(View container) {
}
}
}
Set the listener on the image inside instantiateItem():
@Override
public Object instantiateItem(View collection, int position) {
final LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.my_layout, null);
final ImageView image = (ImageView)layout.findViewById(R.id.image_display);
final int cPos = position;
image.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
ImageView i = (ImageView)v;
if(cPos == 0)
{
//...
}
//...
}
});
return layout;
}
Alternatively, you could use the ImageView.setTag()
method to include data about what Activity to launch. e.g.
if(position == 0) image.setTag("com.blah.android.SomeActivity");
if(position == 1) image.setTag("com.blah.android.AnotherActivity");
//...
And the inside the onClick() above have this instead:
ImageView i = (ImageView)v;
String activityClassName = (String)i.getTag(); // Get the info we stored in the tag.
MyActivity.this.startActivity((new Intent()).setClassName(MyActivity.this, activityClassName));
Note that here you don't actually need the cast to ImageView, since getTag()
is a method of View
. You also don't need a separate OnClickListener for each ImageView. You could just create a single instance of an OnClickListener that grabs the tag data as above, and launches the appropriate activity. Set this OnClickListener on every ImageView inside instantiateItem()
.
P.S. I strongly recommend, if you are downloading images, that you look at some of the image downloaders that have been written for Android. e.g. https://github.com/nostra13/Android-Universal-Image-Loader
-
But how can you tell which image is clicked? Shouldn't you have to add something like "if(position == 0)" or something? – Kuwame Brown Oct 10 '12 at 7:05
-
The "v" in "public void onClick(View v)" is the image that was clicked. A new OnClickListener is set directly on each ImageView. – Jarrod Smith Oct 10 '12 at 7:10
-
Can you write the whole sample, how can you apply like, if the image 1 is clicked, I want it to open the Activity1, or if image2 is clicked, I want it to open the Activity2. And where should I put it on my code though? – Kuwame Brown Oct 10 '12 at 7:25
-