GridView.setOnItemClickListener触发整行

时间:2017-10-24 16:44:55

标签: java android baseadapter android-gridview

我有一个网格视图3x3。在这个网格中我有ImageView s。

我设置OnItemClickListener但是当我点击ImageView时,无论我按哪一列,只有第一个连续按下。我也可以在ImageView(空白区域)之外按下,但按下该行的第一个ImageView

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);
    }
});

这是我的适配器

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只是一个选择的随机图像。

1 个答案:

答案 0 :(得分:2)

我遇到过这个问题,我想出的解决方案很简单:

在适配器中为每个视图提供唯一的TAG

view.setTag(id); //example

在ItemClickListener上只需匹配每个视图的标签,你就会发现这绝不会发生:

if(view.getTag().equals(id))
    doStuff();
if(view.getTag().equals(otherId))
    doOtherStuff();

等等。

以下是一个例子:

<强>适配器

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;
}

<强>活动

@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
    }
}