如何将imageview传递给片段中的方法?
问题描述:
基本上,当我尝试将imageview或视图传递给片段中的方法时,结果为空。到目前为止,我能做的最好的代码至少不会崩溃。我正在夸大两个片段。哪里不对?如何将imageview传递给片段中的方法?
布局有两个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>
片段CODE
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
方法?
答
不能看到布局代码,我建议你可以使用回调。
在你的活动类中实现一个接口,其中它的方法是你想要执行的将要修改或使用图像视图的逻辑。然后,而不是将imageview传递给你的onShareItem,你可以通过'this'并用你需要的任何参数调用回调。
+0
你能给我一个示例代码吗? – afazolo
答
您不能在onViewCreated中创建image_preview,因为它属于Activity并且尚未创建。
覆盖在片段onActivityCreated有你有找到活动中的ImageView的,您使用的是onViewCreated的看法是你的片段布局:
getActivity()findViewById ...
我们需要要查看布局代码,最初的建议是使用onViewCreated来查找视图而不是onCreateView。 OnViewCreated有已经充实的视图作为参数,oberwrite按ctrl + o – cutiko