起初我使用
gallery.setAdapter(new ImageAdapter(this));
public class ImageAdapter extends BaseAdapter{
//声明变量
private Context mContext;
//构造函数
public ImageAdapter(Context c){
mContext= c;
}
//返回图片的数目
@Override
public int getCount() {
// TODO Auto-generated method stub
return pic.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
///imagePosition = position;
///Log.i(tag,"imagePosition= "+imagePosition);
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//获得一个View对象
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//Log.i(tag,"进入getView");
//imagePosition = position;
//Log.i(tag,"imagePosition= "+imagePosition);
//返回Image对象
return fImageView;
}
希望能充ImageAdapter中获得position,在测试时发现这个position非常不准,可谓是变幻莫测。所以我又通过另外一种方法获得position,目前测试还是比较准确的。
//当“Item”被选择的监听器
private class galleryListener implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long id) {
// TODO Auto-generated method stub
Log.i(tag,"进入onItemSelected");
imagePosition = position;
Log.i(tag,"imagePosition= "+imagePosition);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}