我想使用Glide圖像庫動態地將圖像添加到ViewPager中。我有一個CustomPagerAdapter類: -將圖像動態添加到包含ImageView的Viewpager中(個別頁面)
public class CustomPagerAdapter extends PagerAdapter {
private Context mContext;
private LayoutInflater mLayoutInflater;
private List<DressDetailResult> mResources;
public CustomPagerAdapter(Context context, List<DressDetailResult> images) {
this.mContext = context;
this.mLayoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mResources = images;
}
@Override
public int getCount() {
return this.mResources.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.item_dress_detail_vertical_vp, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.item_dress_detail_vertical_vp_iv_dress);
DressDetailResult dressDetailResult = mResources.get(position);
Log.e("image url detail", " " + dressDetailResult.getImages());
Glide.with(mContext).load(dressDetailResult.getImages())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
}
}
這是我的模型類與改造工作2.0: -
package com.webmavens.sherrihill.models.DressDetail;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class DressDetailResult {
@SerializedName("images")
@Expose
private List<String> images = null;
public List<String> getImages() {
return images;
}
public void setImages(List<String> images) {
this.images = images;
}
}
獲取列表中的圖片網址,但不知道如何將其加載到滑翔即使我嘗試使用下面的代碼加載,但只加載一個圖像,而不是增加一個頁面下一張圖片:
List<String> stringList = dressDetailResult.getImages();
Glide.with(mContext).load(stringList.get(position))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
logcat的ImageUrls數組的是: -
[http://chandrajeet.com/wp-content/uploads/2015/02/3148608-lg.jpg,
http://www.gettyimages.in/gi-resources/images/Embed/new/embed2.jpg,
http://www.thinkstockphotos.in/ts-resources/images/home/TS_AnonHP_462882495_01.jpg,
http://hdwallpapershdpics.com/wp-content/uploads/2016/02/4237684-images.jpg]
我認爲它將所有的數組加載到滑翔中,我如何個性化它?
在適配器中傳遞圖像列表。 mResources應該是數組或圖像的URL列表。它會正常工作。 –
沒有得到你的觀點,我在哪裏添加?在哪一行? –
分享api結束點 –