基本上,当我尝试将图像视图或视图传递给片段中的方法时,结果为空。到目前为止,我能做到的最好的就是下面的代码,至少不会崩溃。我正在膨胀两个片段。怎么了?
布局有两个xml文件:
fragment_image_slider.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black" />
...
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<ImageView
android:id="@+id/sharethis"
android:clickable="true"
...
android:src="@drawable/ic_share" />
...
</RelativeLayout>
</RelativeLayout>
image_fullscreen_preview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
...
<ImageView
android:id="@+id/image_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:scaleType="fitCenter" />
</RelativeLayout>
片段代码
private ImageView deleteThis, shareThis;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_image_slider, container, false);
shareThis = (ImageView) v.findViewById(R.id.sharethis);
...
}
@Override
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);
final ImageView imageViewPreview = (ImageView) view.findViewById(R.id.image_preview);
Image image = images.get(position);
Glide
.with(getActivity())
.load(image.getPhotoUrl())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
})
.into(imageViewPreview);
shareThis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onShareItem(imageViewPreview);
}
});
container.addView(view);
return view;
}
public void onShareItem(ImageView v) {
//ImageView ivImage = (ImageView) v.findViewById(R.id.image_preview); if I use `View v` as parameter the app crashes
Uri bmpUri = getLocalBitmapUri(v); // return null
...
}
如何将此变量传递给onShareItem
方法?