2017-05-27 51 views
0

在這裏我有兩個網址,但是當我運行這個應用程序這個圖像被加載到第二頁意味着第一圖像將加載第二頁後幾秒鐘第二圖像也添加到相同的重疊。我需要把它添加到第二和第三頁的URL,這些添加圖像從URL到viewpager在第二和第三的位置

viewPager = (ViewPager) findViewById(R.id.splash); 
     ImageAdapter adapter = new ImageAdapter(this); 
     viewPager.setAdapter(adapter); 

public class ImageAdapter extends PagerAdapter { 

     Context context; 

     private int[] GalImages = new int[] { 
       R.drawable.slider, 
       R.drawable.slider, 
       R.drawable.slider, 
     }; 

     ImageAdapter(Context context){ 
      this.context=context; 
     } 

     @Override 
     public int getCount() { 
     return GalImages.length; 
     // return 10; 
     } 

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

     @Override 
     public Object instantiateItem(ViewGroup container, int position) { 
      ImageView imageView = new ImageView(context); 
      imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
      imageView.setImageResource(GalImages[position]); 
      new LoadImage(imageView).execute("http://www.gadgetbaazar.com/wp-content/uploads/2016/12/Top-Mobile-Phones.jpg"); 
      new LoadImage(imageView).execute("https://pisces.bbystatic.com/BestBuy_US/store/ee/2015/com/pm/nav_desktops_1115.jpg"); 
      container.addView(imageView, 0); 
      return imageView; 
     } 

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



     private class LoadImage extends AsyncTask<String, String, Bitmap> { 
      ImageView img=null; 
      public LoadImage(ImageView img){ 
       this.img=img; 
      } 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 

      } 
      protected Bitmap doInBackground(String... args) { 
       Bitmap bitmap=null; 
       try { 
        bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent()); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       return bitmap; 
      } 
      protected void onPostExecute(Bitmap image) { 
       if(image != null){ 
        img.setImageBitmap(image); 
       } 
      } 
     } 
    } 
+0

所以,你要加載三種不同的圖像viewpager吧? –

+0

來自抽屜的一張圖片和來自url的其他兩張 –

+0

我不太確定你想要實現什麼,但是你有沒有試過玩這個位置?比方說,如果在instantiateItem()如果位置== 1,加載第一個URL,並且如果位置== 2,加載第二個URL –

回答

1

試試這個

@Override 
     public Object instantiateItem(ViewGroup container, int position) { 
      ImageView imageView = new ImageView(context); 
      imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
      if(position == 0){ 
      imageView.setImageResource(GalImages[position]); 
      }else if(position == 1){ 
      new LoadImage(imageView).execute("http://www.gadgetbaazar.com/wp-content/uploads/2016/12/Top-Mobile-Phones.jpg"); 
      }else if(position ==2){ 
      new LoadImage(imageView).execute("https://pisces.bbystatic.com/BestBuy_US/store/ee/2015/com/pm/nav_desktops_1115.jpg"); 
      } 

      container.addView(imageView, 0); 
      return imageView; 
     } 
相關問題