我正在使用ViewPager来滑动屏幕的一部分(而不是整个视图/页面)。
我可以破解isViewFromObject只返回true然后我的第一个图像出现但我的第二个图像不会加载。对我在这里缺少什么的想法?我想我的isViewFromObject方法或实例化项目中的for循环仍有问题。
注意:有意不使用extends FragmentStatePagerAdapter并扩展常规的PagerAdapter。
private class ScreenSlidePagerAdapter extends PagerAdapter {
public ScreenSlidePagerAdapter() {}
@Override
public Object instantiateItem (ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int RLayoutId;
RLayoutId = R.layout.images_to_show; //an XML with just a LinearLayout
ViewGroup imageLayout = (ViewGroup) inflater.inflate(RLayoutId, null);
for (position = 0; position < mImageURLArraylist.size(); position++) {
container.addView(insertPhoto("http:" + mImageURLArraylist.get(position) ) );
}//end of for loop
return imageLayout;
}
@Override
public boolean isViewFromObject (View view, Object obj) { //Object parameter is received from instantiateItem(ViewGroup, int)
//loads the first image successfully if i just do return true.
//doesn't load any of my images when I do return view == ( (View) obj);
}
//one of four methods that you must override when using pageradapters
@Override
public int getCount() {
//tested in debugging and this has the correct size (2)
return mImageURLArraylist.size(); //the number of pages the adapter will create.
}
@Override
public void destroyItem (ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}//end of ScreenSlidePagerAdapter
我在上面的instantiateItem中执行的insertPhoto方法:
public View insertPhoto(String path){
LinearLayout layout = new LinearLayout(getActivity());
layout.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(getActivity() ).load(path).into(imageView); //tried this but got errors when running > resize(layout.getWidth(), layout.getHeight()), also tried .fit() after .load image wouldn't load
layout.addView(imageView);
return layout;
}
答案 0 :(得分:0)
您需要将视图添加到容器中。尝试更改您的方法,如下所示:
@Override
public Object instantiateItem (ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int RLayoutId;
RLayoutId = R.layout.images_to_show; //an XML with just a LinearLayout
ViewGroup imageLayout = (ViewGroup) inflater.inflate(RLayoutId, container, false);
container.addView(insertPhoto(imageLayout, "http:" + mImageURLArraylist.get(position) ));
return imageLayout;
}
// this needs to be refactored, too many viewgroups. Move all layouts to XML
public View insertPhoto(ViewGroup root, String path){
LinearLayout layout = new LinearLayout(getActivity());
layout.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(getActivity() ).load(path).into(imageView); //tried this but got errors when running > resize(layout.getWidth(), layout.getHeight()), also tried .fit() after .load image wouldn't load
layout.addView(imageView);
root.addView(layout);
return root;
}
如果您的布局全部用XML完成,那么insertPhoto函数只需调用Picasso来加载图像。
答案 1 :(得分:0)
为每个页面调用PagerAdapter的instantiateItem()方法,因此只应将一个页面/子项插入到容器中。
@Override
public Object instantiateItem (ViewGroup container, int position) {
final View page = insertPhoto("http:" + mImageURLArraylist.get(position) );
container.addView(page);
return page;
}
@Override
public boolean isViewFromObject (View view, Object obj) {
return view == obj;
}