问题描述
我正在使用ViewPager来滑动屏幕的一部分(而不是整个View/Page).
I'm using a ViewPager to swipe just part of a screen (as opposed to the whole View/Page).
我可以修改isViewFromObject使其返回true,然后出现第一个图像,但第二个图像将无法加载.对我在这里缺少什么有任何想法吗?我在想我的isViewFromObject方法还是实例化项目中的for循环还是有问题.
I can hack isViewFromObject to just return true and then my first image appears but my second image won't load. Any thoughts on what I'm missing here? I'm thinking there's either still something wrong with my isViewFromObject method or my for loop in instantiate item.
注意:故意不使用扩展FragmentStatePagerAdapter和扩展常规PagerAdapter.
Note: Purposefully NOT using extends FragmentStatePagerAdapter and extending regular 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方法:
My insertPhoto method that's executed in instantiateItem above:
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;
}
推荐答案
每页都会调用PagerAdapter的InstantiateItem()方法,因此只能在容器中插入一个页面/子对象.
The PagerAdapter's instantiateItem() method is called for each page, so only one page/child should be inserted into the container.
@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;
}
这篇关于Android ViewPager将不会显示第二页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!