1
我想在Android上創建照片庫。我使用Gallery
和BaseAdapter
來創建可滾動的圖庫。現在我想在顯示圖庫的圖像時添加一個動作(每個圖像的寬度與屏幕寬度相同),因此我需要在圖像數組中獲取其索引。我的問題是如何獲得在屏幕上顯示的圖像的索引?如何在Android上創建照片庫?
我想在屏幕上顯示圖像的索引,而不是點擊事件。 我試圖得到getView()
當前圖像的位置,但結果很奇怪:
我滾動到image02,但位置= 2(應該是1)。當我回滾時,image02的位置= 0(應該是1)。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) this.findViewById(R.id.Gallery);
g.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mImageIds = {
//each image's size is 320x60
R.drawable.btm01,
R.drawable.btm02,
R.drawable.btm03
};
public ImageAdapter(Context c){
this.mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mImageIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(320,60));
return imageView;
}
我想在顯示圖像的索引後立即顯示,而不是點擊事件。那可能嗎? – 2010-11-16 11:51:23
我認爲索引應該從onScroll事件返回。但我找不到任何功能使用。 – 2010-11-16 11:54:59
@iPhoney:你需要什麼?它對我沒有任何意義,但可以在'getView()'方法中使用'position'。 – Macarse 2010-11-16 12:00:20