英文:
How to use Picasso in onCreateView?
问题
I have problem with Picasso code. In my fragment_home (*fragment of navigation bar*) is ImageView where I want to put image from "***image.com***" URL addres.
Picasso code look like that
Picasso.get().load("image.com")
.resize(300,200)
.centerInside()
.into(photo);
I cant write that into my HomeFragmentActivity where code suppose to be because findViewById isn't "working".
*HomeFragmentActivity* > Place where code have to be
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);
}
}
When code is somewhere else (*for example in MainActivity in onCreate method*) app will crash. Please help. Thanks.
**Write below you'r suggests, maybe Picasso code don't must be in HomeFragmentActivity ?**
Please let me know if you need anything else.
英文:
I have problem with Picasso code. In my fragment_home (fragment of navigation bar) is ImageView where I want to put image from "image.com" URL addres.
Picasso code look like that
Picasso.get().load("image.com")
.resize(300,200)
.centerInside()
.into(photo);
I cant write that into my HomeFragmentActivity where code suppose to be because findViewById isn't "working".
HomeFragmentActivity > Place where code have to be
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);
}
}
When code is somewhere else (for example in MainActivity in onCreate method) app will crash. Please help. Thanks.
Write below you'r suggests, maybe Picasso code don't must be in HomeFragmentActivity ?
答案1
得分: 1
你可以像这样声明它:
@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);
}
英文:
You can declare it like this :
@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;
}
or
@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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论