Android PICASSO - Image Not Loading into ImageView + Stops all Statements following from occuring

陌路散爱 提交于 2019-12-02 08:21:39

Use this for old library :-

(implementation 'com.squareup.picasso:picasso:2.5.2')

Picasso.with(this).load(iconUrl).into(Tab1Fragment.weatherIcon);

instead of : For new library

(implementation 'com.squareup.picasso:picasso:2.71828')

Picasso.get().load(iconUrl).into(Tab1Fragment.weatherIcon);

I think you need to set load image from Picasso to ImageView inside Tab1Fragment.

And need to make sure Picasso load to ImageView after fragment already run onCreateView to inflate view and ImageView

I made an example

In activity

  PlaceholderFragment placeholderFragment = (PlaceholderFragment) mSectionsPagerAdapter.getItem(0);
  placeholderFragment.setIcon("http://openweathermap.org/img/w/01d.png");

Fragment

public class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    private ImageView imageView;

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        this.imageView = rootView.findViewById(R.id.imageView);
        return rootView;
    }

    public void setIcon(String url) {
        if (imageView == null) {
            throw new RuntimeException("ImageView null, please make sure this setIcon function run after onCreateView");
        }
        Picasso.get().load(url).into(imageView);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!