我有一個單詞列表。我希望列表中每個單詞的字母在GridView中顯示爲圖像。如何將GridView添加到ListView中
我已經通過爲我的單詞列表創建自定義適配器WordListAdapter來實現了此工作版本。對於每個單詞,WordListAdapter.getView反過來調用一個自定義適配器LetterImageAdapter,以在單詞中顯示字母。由於我是Android編程新手,我想知道這是否是一種好方法。它確實看起來像很多工作。如果這是一個好方法,那麼這對其他人可能會有所幫助。
我見過很多關於ListViews裏GridVeiws的其他文章。他們中的很多人都提到了滾動和內存方面的問題,但我沒有看到這個用例特有的任何問題。
在我的活動:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
ListView listView = (ListView) findViewById(R.id.wordList);
listView.setAdapter(new WordListAdapter(this, new String[]{"ABC", "DEF"}));
}
WordListAdapter.getView()
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.word_grid, null);
GridView gridView = (GridView) convertView.findViewById(R.id.word_grid_id);
holder = new ViewHolder();
holder.gridView = gridView;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.gridView.setAdapter(new LetterImageAdapter(mContext, words[position]));
return convertView;
}
private static class ViewHolder {
GridView gridView;
}
LetterImageAdapter.getView()
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.letter_item, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.list_item_letter_imageview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// resource for each letter is in lettersId array
holder.icon.setImageResource(letterIds[position]);
return convertView;
}
private static class ViewHolder {
ImageView icon;
}
// transform word into array of resources for each letter
private void buildResourceArray(String packageName) {
letterIds = new int[theWord.length()];
for (int index = 0; index < theWord.length(); index++) {
letterIds[index] = getResourceId(theWord.substring(index, index + 1).toLowerCase(), "drawable", packageName);
}
}
謝謝!所以這聽起來好像我需要在LinearLayout中使用一個ListView,這樣我可以遍歷這些圖像。順便說一句,我檢查接受你的答案,但沒有看到投票。 – user6627139
其實我的意思是LinearLayouts圖像作爲ListView項目。我的重點是避免嵌套列表窗口小部件 – rupps