如何从列表视图中单击按钮的位置获取正确的图像视图

How to get correct imageview from the position where button is clicked in Listview

我有一个带有图像和文本的自定义列表视图。我正在使用 Glide 库将图像加载到 Listview 中。有时只有文字而没有图像。现在,我想使用共享意图共享图像视图和文本,为此我创建了一个按钮。我为按钮创建了一个 Onclick 侦听器,它与列表视图中的每个项目一起显示。当我单击按钮时,它将发送 imageview 进行处理,然后转换为位图,然后将其共享。 这里的问题是,当我单击按钮时,未选择特定位置的图像视图,而是选择了不同的图像视图。请帮忙。

我在 Imageview 上有 OnItem 侦听器以全屏和缩放显示它。

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

        final descusers du = dusers.get(position);
        String username = du.loginname;
        String descCrip=du.descCrip;
        Limage = du.image;
        long Ptime = du.Ptime;
        profile = du.profile;


        if(convertView==null) {
            viewholder = new Viewholder();
            convertView = LayoutInflater.from(ds).inflate(R.layout.customlist, null);
            viewholder.uname = (TextView) convertView.findViewById(R.id.username);
            viewholder.desc = (TextView) convertView.findViewById(R.id.description);
            viewholder.time = (TextView)convertView.findViewById(R.id.time);
            viewholder.iview = (ImageView) convertView.findViewById(R.id.imageList);
            viewholder.civ = (CircleImageView)convertView.findViewById(R.id.profile_image);
            viewholder.share = (TextView)convertView.findViewById(R.id.share);

            Glide.with(convertView.getContext()).load(du.profile).placeholder(R.drawable.place).dontAnimate()
                    .centerCrop().signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
                    .into(viewholder.civ);

            convertView.setTag(viewholder);
        }else {

            viewholder = (Viewholder) convertView.getTag();

        }

        viewholder.uname.setText(username);
        viewholder.desc.setText(descCrip);

        if (!Patterns.WEB_URL.matcher(Limage).matches()) {
           // viewholder.progress.setVisibility(View.GONE);
            viewholder.iview.setVisibility(View.GONE);
        } else {

            Glide.with(convertView.getContext()).load(Limage).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).into(viewholder.iview);
            viewholder.iview.setVisibility(View.VISIBLE);
        }


        final View finalConvertView = convertView;
        viewholder.iview.setOnClickListener(new View.OnClickListener() {

             @Override
             public void onClick(View v) {

                Dialog builder = new Dialog(listdisplay, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
                 builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
                 builder.getWindow().setBackgroundDrawable(
                         new ColorDrawable(Color.BLACK));
                 builder.setContentView(R.layout.imager);

                 ImageView image = (ImageView) builder.findViewById(R.id.image);

                 Glide.with(finalConvertView.getContext()).load(du.image)
                         .diskCacheStrategy(DiskCacheStrategy.ALL).into(image);

                 builder.show();
             }
         });

        viewholder.share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //onShareItem(v);
                Uri bmpUri = getLocalBitmapUri(viewholder.iview);
                if (bmpUri != null) {
                    // Construct a ShareIntent with link to image
                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                    shareIntent.setType("image/*");
                    // Launch sharing dialog for image
                    listdisplay.startActivity(Intent.createChooser(shareIntent, "Share Image"));

                } else {
                    // ...sharing failed, handle error
                }
             }
        });


        return convertView;
    }

如何从点击按钮的位置得到正确的Imageview。

您可以在

中使用位置
         **@Override
        public void onClick(View v) {

         }**

它给出了列表项的正确点击位置。

使用此代码 @覆盖 public void onClick(View v) {

    Dialog builder = new Dialog(listdisplay, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
    builder.getWindow().setBackgroundDrawable(
            new ColorDrawable(Color.BLACK));
    builder.setContentView(R.layout.imager);

    ImageView image = (ImageView) builder.findViewById(R.id.image);

    Glide.with(finalConvertView.getContext()).load(du.image)
            .diskCacheStrategy(DiskCacheStrategy.ALL).into(image);

    Glide
            .with(finalConvertView.getContext())
            .load(du.image)
            .asBitmap()
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                    image.setImageBitmap(resource); // Possibly runOnUiThread()
                    profileImage=resourec;
                }
            });

    builder.show();
}

});