图库上的 setSelection 导致选择了错误的项目

发布于 2024-11-24 03:55:10 字数 3372 浏览 3 评论 0 原文

我已经使用 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;
        }
    }

图库如下所示:

在此处输入图像描述

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

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

                    }
                });

但结果是错误的。如果我选择项目编号。 2、货号尽管 setSelection 操作是针对项目 2 触发的,但 3 已被选中。如果我单击上图最右侧的项目,则结果行如下:

在此处输入图像描述

我的代码有什么问题?

I have set up a gallery for my app using BaseAdapter. Here is the code I used for the gallery.

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

The gallery looks like below:

enter image description here

On finger movement, it is moving right to left or left to right as expected. Now I want to add some onClick action to items. If user click on any image, it will be selected and align in the middle. The following code is used for this action.

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

                    }
                });

But it results wrongly. If I am selecting item no. 2, the item no. 3 got selected, although setSelection action is firing against item no 2. If I click on the right most item of the above pic, it is resulting line below:

enter image description here

What is the problem in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽手叙旧 2024-12-01 03:55:11

我自己正在与画廊合作,但我不太确定这里的问题是什么,因为您的代码比我所在的位置更远。

无论如何,既然你点击了第 3 项?并且您将第 4 项居中,我可以想到几个选项:

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

- 就我个人而言,我使用一个 Integer 数组,在 getView() 上我只是得到了比你所做的更容易的位置,我相信。

公共ArrayList mImageIds =新ArrayList();数组...
i.setImageResource(mImageIds.get(position));并在 getView() 中对其进行排序。

希望这有帮助

I'm working with a Gallery myself, but I'm not quite sure what the problem is here, since your code is further than where I stand.

Anyways, since you click item 3?? and you get item 4 centered I can think of a few options:

-Something is wrong with the array indexes, might wanna take a log of the positions.

-Personally, I work with an array of Integer and on the getView() I just get the position which is easier than what you're doing I believe.

public ArrayList mImageIds = new ArrayList(); the array...
i.setImageResource(mImageIds.get(position)); and in getView() it's sorted.

Hope this is helpful

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文