Android ViewPager 不会显示第二页?

Posted

技术标签:

【中文标题】Android ViewPager 不会显示第二页?【英文标题】:Android ViewPager Won't Show Second Page? 【发布时间】:2016-04-24 20:44:45 【问题描述】:

我使用 ViewPager 只滑动屏幕的一部分(而不是整个视图/页面)。

我可以破解 isViewFromObject 以返回 true,然后我的第一张图片出现,但我的第二张图片不会加载。关于我在这里缺少什么的任何想法?我认为我的 isViewFromObject 方法或实例化项中的 for 循环仍然有问题。

注意:故意不使用扩展 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

我在上面实例化项目中执行的 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;        

【问题讨论】:

【参考方案1】:

每个页面都会调用 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;
    

【讨论】:

成功了!非常感谢吉索!如果可以的话,愚蠢的后续问题,如果我将视图添加到线性布局中,我需要一个 for 循环。为什么我不在这里/它如何在没有 for 循环的情况下遍历所有项目(即图像)? @Noobprogrammer:它没有。 ViewPager 的工作方式不同。事实上,它甚至不会立即加载所有页面。相反,它只实例化相邻页面,甚至销毁更远的页面以节省内存。可以使用setOffscreenPageLimit()控制在内存中保存多少页【参考方案2】:

您需要将视图添加到容器中。尝试更改您的方法,如下所示:

    @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 来加载图像。

【讨论】:

以上是关于Android ViewPager 不会显示第二页?的主要内容,如果未能解决你的问题,请参考以下文章

CoordinatorLayout 状态栏填充从 ViewPager 第二页消失

Android Viewpager保存数据和视图

android 卡片画廊效果及RecycleView、ViewPager、ScrollView之前的冲突解决

使用viewpager时,片段不会全屏显示

Android ViewPager,片段中的cameraview未显示

网页的表格是用html写的,打印预览的时候分成两页,第二页的边框显示不全。