Image View is opening a wrong image when clicked on thumbnail image in grid view android

血红的双手。 提交于 2019-12-11 17:40:11

问题


I have some images which is displayed in grid View. If I click on thumbnail image in grid view sometimes it opens a wrong image.

Please note: Not all the time.

This issue i'll face only when I take a picture and goto grid View and trying to open the image which I recently took. For example I have 99 images in grid view and I take 100th photo from my camera app. When I click on 100th image in grid view. It opens the 98 or 97th photo, not the one which I clicked. And it happens vice-verse also. But rest all the thumbnail images are opening fine.

My RecyclerViewAdapter class

 public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) {
        Uri uri = Uri.fromFile(new File(String.valueOf(getItem(position))));
        holder.image.setImageBitmap(null);

        Picasso.with(holder.image.getContext())
                .load(uri)
                .into(holder.image);

        holder.image.setOnClickListener(new OnImageClickListener(position));
    }

    class OnImageClickListener extends BaseActivity implements View.OnClickListener {
        int position;

        public OnImageClickListener(int position){
            this.position = position;
        }

        @Override
        protected int getLayoutResource() {
            return R.layout.activity_recycleview;
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(activity, ImageDisplayActivity.class);
            intent.putExtra("id", getPosition(position));
            activity.startActivity(intent);
        }
    }
    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public Object getItem(int position) {
        return this.itemList.get(getPosition(position));
    }

    public int getPosition(int position) {
        return itemList.size() - 1 - position;
    }

My FullScreenImageAdapter class

 public Object instantiateItem(ViewGroup container, int position) {
        TouchImageView imageView;

        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.layout_fullscreen_image, container, false);

        imageView = (TouchImageView) view.findViewById(R.id.imgDisplay);

        Uri uri = Uri.fromFile(new File(String.valueOf(imagePaths.get(position))));

        // If Check the path here I'm getting the path which image view is displaying, not the image path which I clicked.

        Picasso.with(container.getContext())
                .load(uri)
                .into(imageView);

        container.addView(view);

        return view;
    }

来源:https://stackoverflow.com/questions/28742231/image-view-is-opening-a-wrong-image-when-clicked-on-thumbnail-image-in-grid-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!