在Gallery上的setSelection导致选择了错误的项目

时间:2011-07-18 06:24:05

标签: android onclick gallery adapter

我使用BaseAdapter为我的应用设置了一个图库。这是我用于图库的代码。

homeGallery = (Gallery) findViewById(R.id.homeimggallery);
homeGallery.setSpacing(0);
homeGallery.setSelection(0);
homeGallery.setAdapter(new AddImgAdp(this));

private class AddImgAdp extends BaseAdapter {
     private int GalItemBg, temp;
     private Context cont;
     private View convertView1;
     private ViewGroup parent1;
     private Bitmap mask = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.mask);
     private Bitmap whiteBorder = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.stroke);
     private Bitmap blueBorder = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.strokeactive);
     private Bitmap src;
     private Bitmap dest;
     private Canvas canvas;
     private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
     private ImageView homeImgView;
     private ImageView[] homeImgViewArr= new ImageView[arrThumbImage.length];


     public AddImgAdp(Context c) {
       cont = c;
       TypedArray typArray = obtainStyledAttributes(styleable.GalleryTheme);
       GalItemBg = typArray.getResourceId(styleable.GalleryTheme_android_galleryItemBackground, 0);
       typArray.recycle();
       paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
     }
        public int getCount() {
            return arrThumbImage.length;
        }
        public Object getItem(int position) {
            return position;
        }
        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            homeImgView = new ImageView(cont);

            try{    

                src = BitmapFactory.decodeResource(mContext.getResources(), arrThumbImage[position]);

                dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
                canvas = new Canvas(dest);
                canvas.drawColor(Color.TRANSPARENT);
                canvas.save();
                canvas.translate((canvas.getWidth() - src.getWidth())>> 1, (canvas.getHeight() - src.getHeight())>> 1);
                canvas.drawBitmap(src, 0, 0, null);
                canvas.drawBitmap(mask, 0, 0, paint);
                canvas.drawBitmap(dest, 0, 0, paint);

                homeImgView.setBackgroundDrawable(new BitmapDrawable(dest));            
                homeImgView.setImageResource(R.drawable.stroke);

                homeImgView.setScaleType(ImageView.ScaleType.FIT_XY);
                homeImgViewArr[position] = homeImgView;

            } catch(Exception e) {} 


            return homeImgView;
        }
    }

图库如下所示:

enter image description here

在手指移动时,它会按预期从右向左或从左向右移动。现在我想为项目添加一些onClick操作。如果用户单击任何图像,它将被选中并在中间对齐。以下代码用于此操作。

homeGallery.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView parent, View v, int position, long id) {
homeGallery.setSelection(position);

                    }
                });

但结果错误。如果我选择项目号。 2,项目号。虽然setSelection操作针对第2项触发,但是3被选中了。如果我点击上面图片中最右边的项目,它就是下面的结果:

enter image description here

我的代码中有什么问题?

1 个答案:

答案 0 :(得分:0)

我自己正在与一个画廊合作,但我不太确定这里的问题是什么,因为你的代码比我的立场更进一步。

无论如何,既然你点击第3项?如果项目4居中,我可以考虑几个选项:

- 数组索引有问题,可能需要记录位置。

-Personally,我使​​用了一个Integer数组,并且在getView()上我得到的位置比我相信的更容易。

public ArrayList mImageIds = new ArrayList();阵列...... i.setImageResource(mImageIds.get(位置));并在getView()中进行了排序。

希望这是有帮助的