ViewPager无法与毕加索懒洋洋地加载图像 [英] ViewPager unable to load images lazily with Picasso

查看:245
本文介绍了ViewPager无法与毕加索懒洋洋地加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 FragmentActivity 中的第一个标签一个 TabHost FragmentActivity 本身持有 ViewPager 。我们的目标是有可滑动浏览图像的 TabHost 第一个窗格。该的 setAdapter()方法设置一个 FragmentPagerAdapter ViewPager 用一组片段秒。

I have a FragmentActivity in the first tab of a TabHost, and the FragmentActivity itself holds a ViewPager. The goal is to have swipeable images in the first pane of the TabHost. The ViewPager's setAdapter() method sets a FragmentPagerAdapter with a set of Fragments.

要测试它,我第一次加载大量的图片,我已经在项目的绘制目录本地保存。 精美的一切工作。

To test it, I was first loading a bunch of images that I had kept locally in the project's drawable directory. Everything worked beautifully.

现在我下载了一堆图像的URL的掀起了REST Web服务。我想这些图像在懒洋洋地加载 ViewPager ,和我打过电话毕加索的负荷()方法在三个地方:

Now I'm downloading a bunch of image URLs' off a REST web service. I want these images to load lazily in the ViewPager, and I tried calling Picasso's load() method in three places:


  1. 在的 ViewPager 片段 onCreateView()方法 S(同一个地方,我是从本地绘制目录更早加载图像)。

  1. The onCreateView() method of the ViewPager's Fragments (the same place where I was earlier loading images from the local drawable directory).

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {                

    View myFragmentView = inflater.inflate(R.layout.layout_fragment, container, false);

    ImageView imageView = (ImageView)getView().findViewById(R.id.fragment_image);
    String url = getArguments().getString(IMAGE_RESOURCE_URL);
    Context context = getActivity();
    Picasso.with(context).load(url).fit().into(imageView);

    return myFragmentView;
}


  • 在的 ViewPager 片段 onViewCreated()方法秒。

  • The onViewCreated() method of the ViewPager's Fragments.

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){
    
        Context context = getActivity();
        String url = getArguments().getString(IMAGE_RESOURCE_URL);
        ImageView imageView = (ImageView)view.findViewById(R.id.fragment_image);
        Picasso.with(context).load(url).fit().into(imageView);      
    
    }        
    


  • onInstantiateItem()方法 FragmentPagerAdapter

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
    
        View v = inflater.inflate(R.layout.layout_fragment, container, false);
        Context context = Tab1Activity.this;
        String url = getItem(position).getArguments().getString("IMAGE_RESOURCE_URL");
        ImageView imageView = (ImageView)v.findViewById(R.id.fragment_image);
        Picasso.with(context).load(url).fit().into(imageView);
        return v;
    }
    


  • 这些方法都不奏效。我知道,它不是与毕加索问题,因为有一天,我想在一个使用毕加索的ListView 和它的工作就像一个魅力。我在做什么错了?

    None of these methods worked. I know that its not a problem with Picasso because the other day I tried using Picasso in a ListView and it worked like a charm. What am I doing wrong ?

    推荐答案

    您第一个方法应该可以正常工作......如果你正确地执行它。我希望你的code一起崩溃 NullPointerException异常

    Your first approach should work fine... if you implement it correctly. I would expect your code to crash with a NullPointerException.

    替换为:

    ImageView imageView = (ImageView)getView().findViewById(R.id.fragment_image);
    

    ImageView imageView = (ImageView)myFragmentView.findViewById(R.id.fragment_image);
    

    这篇关于ViewPager无法与毕加索懒洋洋地加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆