Android 让Gridview里的item显示或隐藏

刘征隆 发布于 2014/02/21 12:23
阅读 3K+
收藏 0
当我点击第一的时候,假设第一的position为0,当代码运行完毕后,该汉字会在第二中显示,然后第一的图画和汉字都会隐藏,当我点击第二的position2时候,第一被隐藏的东西要出现。



private GridView Grid_Game, Grid_Game_Press;
public static final String[] WORD = { "不", "流", "泪", "的", "机", "场", "你",
"我", "他", "山", "水", "老", "火", "谢", "上", "了", "拉", "大", "一", "要",
"爱", "广", "死", "话" };
public static String PRESS[] = {};
SimpleAdapter simpleAdapter;
public static int image1[] = { R.drawable.word1 };
public static int image2[] = { R.drawable.word2 };

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PRESS = new String[6];

//第一
Grid_Game = (GridView) findViewById(R.id.grid_game);
Grid_Game.setAdapter(getAdapter(WORD, image1));
Grid_Game.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
for (i = 0; PRESS[i] != null; i++) {
}
switch (position) {
case 0:
PRESS[i] = WORD[position];
Grid_Game_Press.setAdapter(getPress(PRESS, image2));
                                         arg1.setVisibility(View.GONE);
break;
case 1:
PRESS[i] = WORD[position];
Grid_Game_Press.setAdapter(getPress(PRESS, image2));
break;
case 2:
PRESS[i] = WORD[position];
Grid_Game_Press.setAdapter(getPress(PRESS, image2));
break;
case 3:
PRESS[i] = WORD[position];
Grid_Game_Press.setAdapter(getPress(PRESS, image2));
break;
                                             ... ... ... 
                                             ... ... ... 
                                             ... ... ... 
case 23:
PRESS[i] = WORD[position];
Grid_Game_Press.setAdapter(getPress(PRESS, image2));
break;
}
}
});

//第二
Grid_Game_Press = (GridView) findViewById(R.id.grid_game_word);
Grid_Game_Press.setNumColumns(6);
Grid_Game_Press.setAdapter(getPress(PRESS, image2));
// 已选的答案
Grid_Game_Press.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int position2, long arg3) {
switch (position2) {
case 0:
PRESS[position2] = null;
Grid_Game_Press.setAdapter(getPress(PRESS, image2));
break;
case 1:
PRESS[position2] = null;
Grid_Game_Press.setAdapter(getPress(PRESS, image2));
break;
case 2:
PRESS[position2] = null;
Grid_Game_Press.setAdapter(getPress(PRESS, image2));
break;
case 3:
PRESS[position2] = null;
Grid_Game_Press.setAdapter(getPress(PRESS, image2));
break;
case 4:
PRESS[position2] = null;
Grid_Game_Press.setAdapter(getPress(PRESS, image2));
break;
case 5:
PRESS[position2] = null;
Grid_Game_Press.setAdapter(getPress(PRESS, image2));
break;
}
}
});



public SimpleAdapter getPress(String[] pressword, int[] image) {
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < pressword.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("IMAGE", image[0]);
map.put("WORD", pressword[i]);
data.add(map);
}
simpleAdapter = new SimpleAdapter(this, data, R.layout.griditem_level,
new String[] { "IMAGE", "WORD" }, new int[] { R.id.image_level,
R.id.text_level });
return simpleAdapter;
}

public SimpleAdapter getAdapter(String[] word, int[] image) {
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < word.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("IMAGE", image[0]);
map.put("WORD", word[i]);
data.add(map);
}
simpleAdapter = new SimpleAdapter(this, data, R.layout.griditem_level,
new String[] { "IMAGE", "WORD" }, new int[] { R.id.image_level,
R.id.text_level });
return simpleAdapter;
}


}
加载中
返回顶部
顶部