As the title says I want to know the exact position of the item when I click on a view that is inside the item.
Suppose I have the following code within the getView() method from ArrayAdapter:
...
holder = new ViewHolder ();
holder.iconAction = (ImageView)convertView.findViewById (R.id.download_item_iconAction);
holder.iconAction.setOnClickListener (new View.OnClickListener(){
@Override
public void onClick (View v){
//Item X is clicked
}
});
...
Within onClick() I know the view that is clicked, v, but I don't know the position of the item.
Let's do a trick. I'm going to save the position in a ViewHolder when getView() creates the view:
public View getView (int position, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null){
holder = new ViewHolder ();
holder.iconAction = (ImageView)convertView.findViewById (id);
holder.iconAction.setOnClickListener (new View.OnClickListener(){
@Override
public void onClick (View v){
int pos = (Integer)v.getTag ();
}
});
holder.iconWait.setTag (position);
...
}else{
holder = (ViewHolder)convertView.getTag ();
}
...
}
This code works... but not always. If you have to scroll the list to see all the items the views are recycled. Suppose that the list has 10 elements and only 5 are visible (visible means: if we see 1 pixel line of an item, then this item is visible). Now, if we scroll down we will see the sixth element but the first (0) is still visible. We scroll a little more and the first element will be hidden and we will see that the seventh element appears, BUT the view of this new element is the view of the first element (0).
So I'm saving the positions: 0, 1, 2, 3, 4 and 5. The seventh element (6) will have saved the position 0: Wrong.
Another way to get a click callback is using the ListView's OnItemClickListener listener:
listView = getListView ();
listView.setOnItemClickListener (new AdapterView.OnItemClickListener(){
@Override
public void onItemClick (AdapterView<?> parentView, View childView, int position, long id){
...
}
});
If I scroll down I get the exact position, but with this way I receive callbacks when the item is clicked, no matter the clicked child view.
Thanks.