java - Viewholder onClickListener 在 android 中引用了错误的 View

标签 java android share android-viewholder

我有一个 recyclerView,它使用 picasso 从 firebase 加载图像。每个项目(图像)下面都有一个 shareButton,单击该按钮时,会将 View 转换为位图并使用共享 Intent 与其他应用程序共享。问题是它共享下一个图像(项目),而不是共享按钮所在的图像。 这是位图转换和共享的代码-

// Can be triggered by a view event such as a button press
public void onShareItem(View v) {
    // Get access to bitmap image from view
    ImageView ivImage = (ImageView) findViewById(R.id.post_image);
    // Get access to the URI for the bitmap
    Uri bmpUri = getLocalBitmapUri(ivImage);
    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
        startActivity(Intent.createChooser(shareIntent, "Share Image"));

    } else {

    }
}

// Returns the URI path to the Bitmap displayed in specified ImageView
public Uri getLocalBitmapUri(ImageView imageView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;
    if (drawable instanceof BitmapDrawable){
        bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
        return null;
    }
    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file =  new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}

分享按钮如何设置-

        protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
            viewHolder.setTitle(model.getTitle());

            viewHolder.setImage(getApplicationContext(), model.getImage());


            viewHolder.mShareButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    onShareItem(view);



                }
            });


        }
    };

    mBlogList.setAdapter(firebaseRecyclerAdapter);


}

public static class BlogViewHolder extends RecyclerView.ViewHolder {
    View mView;

    Button mShareButton;


    public BlogViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
        mShareButton = (Button) mView.findViewById(R.id.btn_share);
    }

这就是我添加数据的方式

 public static class BlogViewHolder extends RecyclerView.ViewHolder {
    View mView;

    Button mShareButton;


    public BlogViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
        mShareButton = (Button) mView.findViewById(R.id.btn_share);
    }


    public void setTitle(String title) {
        TextView post_title = (TextView) mView.findViewById(R.id.post_title);
        post_title.setText(title);
    }

    public void setImage(final Context ctx, final String image) {


        final ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);


        Picasso.with(ctx)
                .load(image)
                .networkPolicy(NetworkPolicy.OFFLINE)
                .into(post_image, new Callback() {
                    @Override
                    public void onSuccess() {


                    }

                    @Override
                    public void onError() {
                        Picasso.with(ctx)
                                .load(image)
                                .error(R.drawable.header)
                                .placeholder(R.drawable.progress_animation)
                                .into(post_image);
                    }


                });


    }


}

}

最佳答案

为什么不直接将图像本身传递给共享方法?

protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
            viewHolder.setTitle(model.getTitle());
            viewHolder.setImage(getApplicationContext(), model.getImage());
            viewHolder.mShareButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onShareItem(model.getImage());
                }
            });
        }

然后直接分享图片uri。

// Can be triggered by a view event such as a button press
public void onShareItem(String image) {
        Uri imageUri = Uri.parse(image);

        // Construct a ShareIntent with link to image
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
        shareIntent.setType("image/*");

        // Launch sharing dialog for image
        startActivity(Intent.createChooser(shareIntent, "Share Image"));
}

关于java - Viewholder onClickListener 在 android 中引用了错误的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42375127/

相关文章:

java - 特殊字符输入错误

android - 如何为 Android Q 打开带有特定相册或文件夹的默认图库应用程序?

java - 如何向 Alfresco Share 上的 JSP 页面添加身份验证

android - 从 Android 应用分享视频

java - 如何使用正则表达式获取子字符串

java - 使用maven将日志级别注入(inject)logback.xml

java - 自定义监听器

来自处理程序中运行的代码的 Android ANR?

java - 安卓、Java : Share a dialog

java - 使 log4j 控制台附加程序为不同的线程使用不同的颜色