ViewPager无法与毕加索懒洋洋地加载图像 [英] ViewPager unable to load images lazily with Picasso
问题描述
我有一个 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 Fragment
s.
要测试它,我第一次加载大量的图片,我已经在项目的绘制
目录本地保存。 精美的一切工作。
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:
-
在的
ViewPager
片段
S(同一个地方,我是从本地onCreateView()
方法绘制
目录更早加载图像)。
The
onCreateView()
method of theViewPager
'sFragment
s (the same place where I was earlier loading images from the localdrawable
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 Fragment
s.
@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屋!