Reputation: 8538
I have made a ScrollView and filled it with ImageViews that have OnClickListeners attached to them, but now when I try to scroll the ScrollView, it seems that the OnClickListeners get in the way and don't give a smooth scroll.
Is there a way to fix that, other then using a ListView instead of ScrollView?
Thanks!
Upvotes: 4
Views: 3050
Reputation: 2047
You don't have to have an OnClickListener for each ImageView.
The callback definition is
public abstract void onClick (View v)
where v
is the view that was clicked.
Depending on what you want to do, you can identify the specific view or not. IF you just want to do something generic, (for example, applying a tint), then this is very straightforward
...
public void onClick(View v) {
ImageView iv = (ImageView) v;
iv.setTint(Color.BLUE); // Or whatever generic operation you want
}
If you need to identify the view specifically, e.g. it's part of an XML menu, then I usually use the itemID to identify it
...
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.save:
//do stuff
break;
case R.id.open:
//do stuff
break;
}
}
If the views aren't static, then you can use the setTag()
and getTag()
methods to uniquely identify (and associate useful data!) with each view:
...
public void onClick(View v) {
// the MyViewInfo object was associated with the view somewhere else, presumably when the view was created
MyViewInfo info = (MyViewInfo) v.getTag();
File datafile = info.getFileName();
...
}
}
Upvotes: 2