如何在 onCreateView 中使用 Picasso?
How to use Picasso in onCreateView?
我对 Picasso 代码有疑问。在我的 fragment_home( 导航栏片段 )是 ImageView,我想在其中放置来自“image.com[= 的图像28=]" URL 地址。
毕加索代码看起来像那样
Picasso.get().load("image.com")
.resize(300,200)
.centerInside()
.into(photo);
我无法将其写入我的 HomeFragmentActivity 中,因为 findViewById 不是 "working"。
HomeFragmentActivity > 必须放代码的地方
public class HomeFragment extends Fragment {
private ImageView photo;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home /*in this layout is ImageView where Picasso is inserting image from URL addres*/) , container, false);
}
}
当代码位于其他地方时(例如在 onCreate 方法中的 MainActivity 中)应用程序将崩溃。请帮忙。谢谢
在下面写下你的建议,也许Picasso代码不一定在HomeFragmentActivity中?
你可以这样声明:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
photo = view.findViewById(R.id.photo);
Picasso.with(context).load("url")
.resize(300,200)
.centerInside()
.into(photo);
return view;
}
或
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
photo = view.findViewById(R.id.photo);
Picasso.with(context).load("url")
.resize(300,200)
.centerInside()
.into(photo);
}
我对 Picasso 代码有疑问。在我的 fragment_home( 导航栏片段 )是 ImageView,我想在其中放置来自“image.com[= 的图像28=]" URL 地址。 毕加索代码看起来像那样
Picasso.get().load("image.com")
.resize(300,200)
.centerInside()
.into(photo);
我无法将其写入我的 HomeFragmentActivity 中,因为 findViewById 不是 "working"。
HomeFragmentActivity > 必须放代码的地方
public class HomeFragment extends Fragment {
private ImageView photo;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home /*in this layout is ImageView where Picasso is inserting image from URL addres*/) , container, false);
}
}
当代码位于其他地方时(例如在 onCreate 方法中的 MainActivity 中)应用程序将崩溃。请帮忙。谢谢
在下面写下你的建议,也许Picasso代码不一定在HomeFragmentActivity中?
你可以这样声明:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
photo = view.findViewById(R.id.photo);
Picasso.with(context).load("url")
.resize(300,200)
.centerInside()
.into(photo);
return view;
}
或
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
photo = view.findViewById(R.id.photo);
Picasso.with(context).load("url")
.resize(300,200)
.centerInside()
.into(photo);
}