问题
I'm using a ViewPager to swipe just part of a screen (as opposed to the whole View/Page).
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.
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
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;
}
回答1:
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;
}
回答2:
You need to add the view to the container. Try changing your method as shown here:
@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;
}
It would also be better if your layout was all done in XML, so the insertPhoto function simply calls Picasso to load the image.
来源:https://stackoverflow.com/questions/36828706/android-viewpager-wont-show-second-page