我正在使用图像切换器,并且想知道如何以编程方式设置我的图像库中的初始选定项目。
当前在激活图像选择活动时,选择始终默认为可用图像数组中的第一个。如果用户之前选择了一个,我想将该项目召回为切换器库中的第一个呈现图像。
在选择时,我将选择 int 发送到一个“全局变量”,我可以在调用活动时使用它来调用。
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery1);
g.setAdapter(new ImageAdapter(this));
int texPositionVariable = 2;
mSwitcher.setImageResource(mImageIds[texPositionVariable]); // should load position 2 but doesn't
g.setOnItemSelectedListener (new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
Log.d("textureselected",String.valueOf(position));
GlobalVariables.setTexture((float)position);
mView.requestRender();
}
@Override
public void onNothingSelected(
AdapterView<?> paramAnonymousAdapterView) {
}
});
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(200, 150));
i.setBackgroundResource(R.drawable.long1);
return i;
}
private Context mContext;
}
private Integer[] mThumbIds = {
R.drawable.textures00, R.drawable.textures01,
R.drawable.textures02, R.drawable.textures03,
R.drawable.textures04, R.drawable.textures05,
R.drawable.textures06, R.drawable.textures07};
谢谢。