我在Android中有一个3 x 3网格视图,我使用自定义适配器使用自定义适配器填充每一行,我在每个ImageView上设置不同的imageview.setImageResource(drawable)。 在每个ImageView上,我还添加了一个onTouchListener(在适配器内部),在操作Down时将图像资源更改为新的图像资源,在Action Up上它将旧图像设置回来,以便模拟按下按钮。
一切正常。除非我单击任何一行,它只会更改最后一行的图像,而不是更改单击的单元格的图像资源。现在我知道这可能是由于一些基本缺乏Android工作原理的知识,但任何人都可以给我一些我可能做错的提示吗?
更新。这是我的适配器代码:
public class ListImageAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mThumbIds;
private int w;
protected int category;
private int movefact;
private ImageView imageView;
public ListImageAdapter(Context c, Integer[] ims) {
mContext = c;
mThumbIds = ims;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext) {
@Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
w = getMeasuredWidth();
}
};
int width;
width = mContext.getResources().getDisplayMetrics().widthPixels / 4;
imageView.setLayoutParams(new GridView.LayoutParams(width, width / 3));
imageView.setScaleType(
ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
imageView.setBackgroundResource(R.drawable.bckborderlistbig2);
imageView.setPadding(8, 8, 8, 8);
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// Log.i("IMAGE", "motion event: " + imageView.toString());
imageView.setImageResource(R.drawable.ghost1);
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
imageView.setImageResource(R.drawable.ghost1);
//imageView.setBackgroundResource(0);
break;
case MotionEvent.ACTION_UP:
imageView.setImageResource(mThumbIds[position]);
break;
}
return false;
}
});
return imageView;
//}
}
}