所以,海報沒有(原來)精確指定的其他活動是否在點擊時立即打開,或者如果點擊的項目圖像只是需要記錄以備後用。第一個問題在我看來更常見,如顯示一組圖像縮略圖的GridView
。用戶點擊它,並顯示一個新的活動,只顯示該項目的信息。該縮略圖圖像可能會全屏顯示。無論如何,也許這不是海報的想法,但那是我的假設(也許有人最終會在考慮到這種用例的情況下找到這個答案)。
也沒有指定圖像的存儲方式,所以我會假設
- 的
ImageAdapter
的圖像被捆綁這個應用程序的資源
- 我們可以做出一些更改
ImageAdapter
,存儲一些數據來解決這個問題
所以,就這樣,我拿一個標準ImageAdapter
,並添加一行代碼記錄每個ImageView的整數資源ID,在的ImageView tag
財產。有很多方法可以做到這一點,但這是我選擇的方式。
public class ImageAdapter extends BaseAdapter {
/* see code removed at
http://developer.android.com/resources/tutorials/views/hello-gridview.html
*/
// 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(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
// here we record the resource id of the image, for easy access later
imageView.setTag(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5
};
}
然後,當你好活動有一個網格項點擊,我檢索圖像的資源ID,並把它作爲一個Intent
額外(HelloGridViewActivity.java):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridView = (GridView) findViewById(R.id.gridview);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
final Context activity = this;
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Object tag = v.getTag();
if (tag instanceof Integer) {
// we stored a reference to the thumbnail image in the ImageView's tag property
Intent i = new Intent(activity, AnotherActivity.class);
Integer resourceId = (Integer)tag;
i.putExtra("backgroundImage", resourceId);
startActivity(i);
}
}
});
}
最後,當新活動(AnotherActivity
)被打開,我們檢索意圖額外的,和它作爲一個整數資源ID進行解碼,並將其設置爲與全屏ImageView
(AnotherActivity.java)的背景圖片:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.another);
Intent i = getIntent();
//String bgImage = i.getExtras().getString("backgroundImage");
int resId = i.getExtras().getInt("backgroundImage");
try {
ImageView background = (ImageView) findViewById(R.id.bgImage);
// Another alternative, if the intent extra stored the resource 'name',
// and not the integer resource id
//Class<?> c = R.drawable.class;
//background.setImageResource(c.getDeclaredField(bgImage).getInt(c));
background.setImageResource(resId);
} catch (Exception e) {
e.printStackTrace();
}
}
我也顯示上述一些註釋掉的代碼,如果由於某種原因,你需要的不是一個整數資源代碼傳遞的圖像資源的串名。註釋掉的代碼查找給定圖像名稱的資源ID。根據ImageAdapter中使用的資源名稱,要傳遞的示例字符串名稱可能是"pic1"
。如果你這樣做,當然,調用Activity(HelloGridViewActivity)需要將Intent extra編碼爲字符串,而不是整數。
如果您以某種方式在GridView中加載圖像,則只需使用Intent將相同的信息(URL或文件URI)發送到第二個活動。 –
@ user1420042:您的'GridView'適配器如何被填充?使用資源ID或文件URI? – Squonk