ViewPager + Picasso + URL字符串数组中的多个图像

时间:2018-12-11 06:03:50

标签: android android-viewpager picasso android-photoview

请帮助我。我正在使用Chris Banes(https://github.com/chrisbanes/PhotoView)的示例PhotoView中的示例ViewPagerActivity,并且我想使用Picasso URL图像更改可绘制对象。因此,我试图通过URL的Picasso String数组在HackyViewPager中放入多个图像。

我的xml ViewPager:

<my.package.HackyViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="200dp" />

在onCreate中:

...      
ViewPager viewPager = (HackyViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(new SamplePagerAdapter());

我有一个从单个String stringDefinitionGemstonesPhoto获得的URL的String数组

String[] strAry = stringDefinitionGemstonesPhoto.split(",");

,SamplePageAdapter中的最终代码为:

static class SamplePagerAdapter extends PagerAdapter {

    private String stringDefinitionGemstonesPhoto = ("https://geology.com/minerals/photos/calcite-marble-127.jpg, https://geology.com/minerals/photos/calcite-concrete.jpg");
    private  String[] strAry = stringDefinitionGemstonesPhoto.split(",");


    @Override
    public int getCount() {
        return strAry.length;
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        PhotoView photoView = new PhotoView(container.getContext());

        System.out.println(strAry[position]);
        Picasso.get().load(strAry[position]).into(photoView);

        container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return photoView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

}

当我滑动时,仅显示一张图像,下一张照片为空/空白。在日志系统中,打印出正确分割的链接。 怎么了?

2 个答案:

答案 0 :(得分:2)

我看错了,那是

int

应该是

for(int i= 0; i < numbers.length; i++){
                System.out.println(strAry[i]);
                System.out.println(strAry[position]);
                Picasso.get().load(strAry[position]).into(photoView);
            }

更新

我发现 System.out.println(strAry[position]); Picasso.get().load(strAry[position]).into(photoView); PicassoGlidesee more

中无法正常工作的问题

所以我的解决方案不再使用上述lib了,我们尝试手动请求url,由于性能问题,这种方法虽然更好,但它确实有效。

这是我的解决方法

instantiateItem()

答案 1 :(得分:2)

无需单独使用数字数组,更新适配器类

static class SamplePagerAdapter extends PagerAdapter {

    private static String[] strAry = stringDefinitionGemstonesPhoto.split(",");


    @Override
    public int getCount() {
        return strAry.length;
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        PhotoView photoView = new PhotoView(container.getContext());

            System.out.println(strAry[position]);
            Picasso.get().load(strAry[position]).into(photoView);

        container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return photoView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

}