我正在开发一个示例应用程序,我需要在onClick侦听器中获取图像视图的资源,并将其与我知道存在的图像源进行比较.如果资源相同,我想启动另一个意图.我现在面临的问题是访问ImageView(以及它的资源Id整数)以与可绘制资源进行比较.
@Override
// should int be final ??
public View getView(final 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.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// This is not working and I need to find a way to solve this >>
if (((ImageView)v).getResources().getInteger(0) == R.drawable.imageToCompare)
{
// do nothing
}
else
{
// do something
}
解决方法:
您无法从ImageView获取可绘制的ID.但是如果你从代码中设置它,你也可以将它存储在某个地方,例如在标记字段中.看看类似的问题:Who I compare an background Image resource TextView with a R.drawable.bg_image for switch.