【问题标题】:How can i access to LayoutInflater imageView我如何访问 LayoutInflater imageView
【发布时间】:2017-05-11 12:50:30
【问题描述】:

这是我的对象

    public Object instantiateItem(ViewGroup container, int position) {

        layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.image_fullscreen_preview, container, false);

        ImageView imageViewPreview = (ImageView) view.findViewById(R.id.image_preview);

        Image image = images.get(position);

        Glide.with(getActivity()).load(image.getLarge())
                .thumbnail(0.5f)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageViewPreview);

        container.addView(view);

        return view;
    }

这是我的 onShare 函数。

公共无效 onShareItem() { // 从视图中获取位图图像

      ImageView  i = (ImageView) getActivity().getLayoutInflater().inflate(R.layout.image_fullscreen_preview,null).findViewById(R.id.image_preview);

    //imageViewPreview = (ImageView) viewPager.findViewById(R.id.image_preview);
    // Get access to the URI for the bitmap
    Uri bmpUri = getLocalBitmapUri(imageViewPreview);
    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 {
        // ...sharing failed, handle error
    }
}

当我调试应用程序时,imageview 为空..

我想在 R.id.image_preview 中分享图片 那么我怎样才能访问 R.id.image_preview 图像。

【问题讨论】:

    标签: android android-imageview layout-inflater


    【解决方案1】:

    为什么不使用类变量来存储 ImageView,以便在类中的任何地方都可以使用它?

    private ImageView mImageView;
    
    public Object instantiateItem(ViewGroup container, int position) {
    
        layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.image_fullscreen_preview, container, false);
    
        mImageView = (ImageView) view.findViewById(R.id.image_preview);
        ...
    }
    
    public void onShareItem() {  
        if (mImageView != null) {
             Uri bmpUri = getLocalBitmapUri(imageViewPreview);
             ...
         }
    }
    

    【讨论】:

    • mImageView = (ImageView) getActivity().getLayoutInflater().inflate(R.layout.image_fullscreen_preview,null).findViewById(R.id.image_preview); Uri bmpUri = getLocalBitmapUri(mImageView); böpUri 为空 :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多