Search code examples
androidselectionimageswitcher

Android Imageswitcher how to set initial selection


Im using an imageswitcher and want to know how to programatically set the initial selected item in my gallery of images.

Currently on activating the image selection activity the selection always defaults to the first in the array of available images. If a user has previously selected one I would like to recall that item as the first presented image in the switcher gallery.

On selection I am sending the selection int to a 'global variable' which I could use to call when the activity is recalled.

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

Thanks.


Solution

  • Question Answered by using 'setSelection(int)' within the gallery.

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

    Thanks