我有一个大致像这样的网格视图(每个图像最终都会有所不同):
当用户单击数组中的任何图像时,我希望该图像更改为此:
如果他们再次单击,它将更改为:
然后再次单击将还原为:
到目前为止,这是我的代码,仅使用Imageadapter创建GridView:
public class GridScroll extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> parent, View v, int position, long id) {
// CHANGE IMAGE HERE
Toast.makeText(GridScroll.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
和:
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.lifestyle_5,R.drawable.lifestyle_6,
R.drawable.lifestyle_7,R.drawable.lifestyle_8,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.lifestyle_1,R.drawable.lifestyle_2,
R.drawable.lifestyle_3,R.drawable.lifestyle_4,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.lifestyle_1,R.drawable.lifestyle_2,
R.drawable.lifestyle_3,R.drawable.lifestyle_4,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.lifestyle_1,R.drawable.lifestyle_2,
R.drawable.lifestyle_3,R.drawable.lifestyle_4,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.icon_4, R.drawable.icon_4,
R.drawable.icon_4, R.drawable.icon_4,
};