0
我開始使用Google的網格視圖示例代碼,但我將如何選擇顯示哪些圖像?假設有一次我只想讓eq1和eq4出現,但是又一次我想要eq3,eq 6和eq12出現,我需要對代碼做些什麼? Integer數組包含來自應用程序的另一部分的一些整數,這些整數不會始終相同,我試圖讓這些整數與網格中的哪些圖像相對應。如何選擇在網格視圖中顯示哪些圖像資源?
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private int length;
private Integer[] choices;
public ImageAdapter(Context c, int inLength, Integer[] inChoices) {
mContext = c;
length = inLength;
choices = inChoices;
}
public int getCount() {
return length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
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(200, 50));
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
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.eq1, R.drawable.eq2,
R.drawable.eq3, R.drawable.eq4,
R.drawable.eq5, R.drawable.eq6,
R.drawable.eq7, R.drawable.eq8,
R.drawable.eq9, R.drawable.eq10,
R.drawable.eq11, R.drawable.eq12,
R.drawable.eq13, R.drawable.eq14,
R.drawable.eq15, R.drawable.eq16,
R.drawable.eq17, R.drawable.eq18,
R.drawable.eq19, R.drawable.eq20,
R.drawable.eq21, R.drawable.eq22
};
}
啊無瑕。我之前嘗試過類似的東西,但我一定會搞砸的。我非常感謝幫助。 – tedbrooks2