我如何访问LayoutInflater imageView

时间:2017-05-11 12:50:30

标签: android android-imageview layout-inflater

这是我的对象

    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功能。

public void 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
    }
}

当我调试app时,imageview为null ..

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

1 个答案:

答案 0 :(得分:0)

为什么不使用类变量来存储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);
         ...
     }
}