2016-04-24 102 views
0

我正在使用ViewPager轻扫一部分屏幕(而不是整个视图/页面)。Android ViewPager不会显示第二页?

我可以破解isViewFromObject只是返回true,然后我的第一个图像出现,但我的第二个图像将不会加载。有什么想法,我在这里失踪?我在想我的isViewFromObject方法或实例化项目中的for循环有问题。

注意:有意不使用扩展FragmentStatePagerAdapter并扩展常规PagerAdapter。这是一个在instantiateItem上述执行

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 

我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

为每个页面调用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; 
    } 
+0

工作正常!非常感谢Giso!愚蠢的后续问题,如果我可以,如果我将视图添加到linearlayout我需要一个for循环。为什么我不在这里/它是如何迭代所有的项目(即图片)而没有for循环? – Noobprogrammer

+0

@Noobprogrammer:它没有。 ViewPager的工作方式不同。实际上,它甚至不会立即加载所有页面。相反,它只是实例化相邻页面,甚至将页面进一步销毁以节省内存。 可以使用[setOffscreenPageLimit()](http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit%28int%29)来控制内存中保存的页数。 –

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功能简单地调用毕加索加载图像。

+0

感谢您花时间Francesc! – Noobprogrammer