Android Imageswitcher如何设置初始选择

时间:2013-11-30 07:22:53

标签: android selection imageswitcher

我使用的是imageswitcher,想知道如何以编程方式设置我的图片库中的初始选定项目。

目前,在激活图像选择活动时,选择始终默认为可用图像数组中的第一个。如果用户之前选择了一个,我想将该项目作为切换器库中第一个呈现的图像。

在选择时我将选择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};

感谢。

2 个答案:

答案 0 :(得分:1)

您正尝试在ImageResource Gallery's之前设置OnItemSelectedListener,但默认情况下Gallery's OnItemSelectedListener会再次ImageResource触发ImageSwitcher's position Gallery mSwitcher.setImageResource(mImageIds[texPositionVariable]); 1}}设置为g.setSelection(texPositionVariable); 为0(零)。您需要将选择设置为{{1}}

试试这个:

替换

{{1}}

{{1}}

答案 1 :(得分:0)

问题通过在图库中使用'setSelection(int)'来回答。

    Gallery g = (Gallery) findViewById(R.id.gallery1);
    g.setAdapter(new ImageAdapter(this));

    // this worked
    float texSelected = GloblaVariables.getTexture();
    g.setSelection((int)texSelected);

    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) {
        }
    });

由于