如何在onCreateView中使用Picasso?

huangapple go评论79阅读模式
英文:

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);
}

huangapple
  • 本文由 发表于 2020年4月4日 03:41:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61019321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定