Android ViewPager加载相同的图片

时间:2017-06-07 10:39:33

标签: android android-viewpager

我正在使用ViewPager从服务器加载图像。当它被刷过时,第一个图像会一次又一次地显示。我检查了浏览器中的网址,它显示了不同的图像。然后我尝试从drawable加载图像。这是最后一个图像正在重复加载。下面是完整的代码:

ImagePagerAdapter.java:

 public class ImagePagerAdapter extends PagerAdapter {
    TaxiDisplayData taxiDisplayData;
    LinearLayout pager_lyt;
    ImageView imageView;
    LayoutInflater layoutInflater;
    Context context;
    String category;
    int count;
    LinearLayout linearLayout;
    View view;
    ImageLoaderConfiguration config;
    ArrayList images;

    public ImagePagerAdapter(Context context,String ctgry){
        Log.e("I","T");
        config = new ImageLoaderConfiguration.Builder(context).build();
        ImageLoader.getInstance().init(config);
        images =new ArrayList();
        this.context=context;
        this.category=ctgry;
        if (category.equalsIgnoreCase("taxi")){
            count= taxiDisplayData.getInstance().imagecount();
        }
        layoutInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }
    @Override
    public int getCount() {

        Log.e("Count",Integer.toString(count));
        return count;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        view=layoutInflater.inflate(R.layout.srch_dsply_pager,container,false);
        linearLayout=(LinearLayout) view.findViewById(R.id.pgr_lyt);
         imageView=(ImageView) view.findViewById(R.id.image1);
        if (category.equalsIgnoreCase("taxi"))
        {  images =taxiDisplayData.getInstance().getArrayList();
            if(count!=0){

                Iterator iterator= images.iterator();
                while (iterator.hasNext())
                {
                    Log.e("I","Z");
                    Picasso.with(context).load("http://example.com/xxxx/"+(String) iterator.next())
                            .placeholder(R.drawable.doctors)
                            .fit().into(imageView);
                }
            }}
        container.addView(view);
        return view;
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        Log.e("I","f");
        return view==object;
    }}

srch_dsply_pager.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pgr_lyt">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="@dimen/prsnl_img_dimen"
    android:id="@+id/image1"
    android:scaleType="fitXY"
   />
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

instantiateItem()创建一个视图并将其添加到视图寻呼机中。在您的源代码中,您将遍历整个数组,仅加载最后一个数组并返回视图。相反,你应该有像

这样的东西
String url = images.get(position);
Picasso.load(url).into(imageview);