Reputation: 1112
I have a grid view 3x3. Inside this grid I have ImageView
s.
I set up OnItemClickListener
but when I click on the ImageView
only the first one is pressed in a row no matter which column I am pressing. Also I can be pressing outside the ImageView
(empty space) but still first ImageView
on that row is pressed.
XML
<GridView
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
MainActivity
GridView gridview = (GridView) findViewById(R.id.test);
gridview.setAdapter(new HexAdapter(getBaseContext()));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.e("gridView", "hallo" + position);
}
});
and this is my adapter
public class HexAdapter extends BaseAdapter {
private Context mContext;
public HexAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return (Object) mThumbIds[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.logo, R.drawable.logo,
R.drawable.logo, R.drawable.logo,
R.drawable.logo, R.drawable.logo,
};
}
R.drawable.logo is just a random image a chose.
Upvotes: 0
Views: 124
Reputation: 9676
I have experienced this problem and the solution I came up with is simple:
In your adapter give an unique TAG to each view
view.setTag(id); //example
And on ItemClickListener just match the tags for each view and you will be sure this will never happen:
if(view.getTag().equals(id))
doStuff();
if(view.getTag().equals(otherId))
doOtherStuff();
And so on.
Here is an example:
Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ImageView imageView;
CustomTextView textView;
LinearLayout main_grl;
if (v == null) {
v = layoutInflater.inflate(R.layout.professions_gridview_item,parent,false);
}
main_grl = (LinearLayout)v.findViewById(R.id.main_grl);
main_grl.setBackground(ContextCompat.getDrawable(mContext,R.drawable.ps_background_selector));
imageView = (ImageView) v.findViewById(R.id.imageView);
textView = (CustomTextView) v.findViewById(R.id.textView);
imageView.setImageResource(ApplicationConstants.finalListIV[position]);
textView.setText(ApplicationConstants.finalListTV[position]);
v.setTag(ApplicationConstants.finalListTV[position]);
return v;
}
Activity
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if((Integer) view.getTag() == someTag){
//doStuff
} else if((Integer) view.getTag() == otherTag){
//doOtherStuff
} else{
//andSomeOther
}
}
Upvotes: 2