我有一个看起来大致相同的网格视图(最后每个图像都是不同的):
当用户点击数组中的任何图像时,我希望该图像更改为:
如果他们再次点击,则更改为:
然后再次点击将恢复为:
到目前为止,这是我的代码,只需使用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,
};
答案 0 :(得分:4)
要做到这一点,我们需要做两件事:
<强> 1。单击项目时更改项目的drawable。
在onItemClick(...)
中,更改传递给您的View的drawable。此视图与您在适配器的getView(...)
中创建的视图相同。
<强> 2。下次进入屏幕时,请确保该项目显示正确的drawable。
为此,请跟踪每个项目的状态。每次在getView(...)
中查看项目时,请为其状态指定正确的drawable。
这是一个例子。我假设ImageAdapter是ArrayAdapter的子类。如果没有,那么您将需要修改此代码以使用您正在执行的操作。
把这些放在某处:
private static final int WHITE = 0;
private static final int TEAL = 1;
private static final int MAROON = 2;
private List<Integer> mStates = new ArrayList<Integer>();
这适用于您的ImageAdapter:
// Map each state to its graphics.
private Map<Integer, Integer> mStateResources = new HashMap<Integer, Integer>();
mStateResources.put(WHITE, R.drawable.white);
public void add(...) {
super.add(...);
// The new item will start as white.
mStates.add(WHITE);
}
public View getView(int position, View convertView, ViewGroup parent) {
//ImageView image = ...
// Set the correct image for the state of this item.
int state = mStates.get(position);
int resId = mStateResources.get(state);
image.setImageResource(resId);
}
在OnItemClickListener中:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Change the image and state for this item.
int nextState;
switch(mStates.get(position)) {
case WHITE:
nextState = TEAL;
break;
case TEAL:
nextState = MAROON;
break;
case MAROON:
nextState = WHITE;
break;
}
// Set the new state and image for this item.
mStates.put(position, nextState);
int resId = mStateResources.get(nextState);
image.setImageResource(resId);
}