【发布时间】:2017-09-23 19:22:29
【问题描述】:
我正在开发一个应用程序,其中有一个项目列表,每个项目都由一个 viewpager 组成,在 viewpager 下方还有其他小部件(文本、按钮...等)。我必须从服务器加载图像的 url 并在我的 viewpager 中显示图像
这是我的 Viewpager 适配器:
public class ImageAdapter extends PagerAdapter {
private Context mContext;
private ArrayList<String> imagePaths;
private static final String TAG = ImageAdapter.class.getName();
// constructor
public FullScreenImageAdapter(Context context, ArrayList<String> imagePaths){
this.mContext = context;
this.imagePaths = imagePaths;
}
@Override
public int getCount() {
return null == imagePaths ? 0 : this.imagePaths.size();
}
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
final LayoutInflater inflater = LayoutInflater.from(mContext);
final View viewLayout = inflater.inflate(R.layout.full_screen_image, container, false);
final ImageView image = (ImageView) viewLayout.findViewById(R.id.image);
final ProgressBar progressBar = (ProgressBar)viewLayout.findViewById(R.id.pBar);
String url = BuildConfig.URL_API_IMAGES;
final SparseArray<Object> mBitmapMap = new SparseArray<>();
Glide.with(mContext)
.load(url + imagePaths.get(position)).asBitmap()
.placeholder(R.drawable.car)
.dontAnimate()
.into(new BitmapImageViewTarget(image){
@Override
public void onLoadStarted(Drawable placeholder){
super.onLoadStarted(placeholder);
image.setImageDrawable(placeholder);
}
@Override
public void onResourceReady(Bitmap resource,
GlideAnimation<? super Bitmap> glideAnimation){
super.onResourceReady(resource, glideAnimation);
mBitmapMap.put(position, resource);
progressBar.setVisibility(View.INVISIBLE);
image.setImageBitmap(resource);
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable){
super.onLoadFailed(e, errorDrawable);
progressBar.setVisibility(View.INVISIBLE);
}
});
container.addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
try{
container.removeView((RelativeLayout) object);
Glide.clear(((View) object).findViewById(R.id.image));
unbindDrawables((View) object);
object = null;
}catch (Exception e) {
Log.w(TAG, "destroyItem: failed to destroy item and clear it's used resources", e);
}
}
protected void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
@Override
public boolean isViewFromObject(View view, Object object){
return view == ((RelativeLayout) object);
}
}
这是我的回收站视图项目的一部分:
<RelativeLayout
android:id="@+id/rl_pictureLayout"
android:layout_width="match_parent"
android:layout_marginLeft="@dimen/dimen_5"
android:layout_marginRight="@dimen/dimen_5"
android:layout_marginTop="@dimen/dimen_5"
android:layout_height="@dimen/sliderLayoutHeight">
<ViewPager
android:id="@+id/vp_photos"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_marginBottom="50dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
/>
<ImageButton
android:id="@+id/ib_bookmark"
android:background="@color/transparent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="@dimen/dimen_16"
android:layout_marginRight="@dimen/dimen_16"
android:layout_marginEnd="@dimen/dimen_16"
android:src="@drawable/ic_bookmark_border_white_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv_picture_counter"
android:textColor="@color/white"
android:textAllCaps="true"
android:fontFamily="sans-serif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="@dimen/dimen_16"
android:layout_marginStart="@dimen/dimen_16"
android:layout_marginBottom="@dimen/dimen_24"
/>
<TextView
android:id="@+id/tv_damagedPhotos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:fontFamily="sans-serif"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="@dimen/dimen_16"
android:layout_marginRight="@dimen/dimen_16"
android:layout_marginBottom="@dimen/dimen_24"
android:background="@color/transparent"
android:drawableRight="@drawable/ic_photo_camera_red_24dp"
android:drawableEnd="@drawable/ic_photo_camera_red_24dp"
android:drawablePadding="@dimen/dimen_5"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_timerLayout"
android:layout_below="@id/rl_pictureLayout"
android:layout_marginTop="@dimen/dimen_5"
android:layout_alignLeft="@id/rl_pictureLayout"
android:layout_alignStart="@id/rl_pictureLayout"
android:layout_alignRight="@id/rl_pictureLayout"
android:layout_alignEnd="@id/rl_pictureLayout"
android:layout_marginBottom="@dimen/dimen_10"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_timer"
style="@style/text_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:textColor="@color/ColorBlack"
android:textStyle="bold"
/>
<TextView
android:id="@+id/tv_highest_bid"
style="@style/text_item"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_bid"
style="@style/button"
android:layout_width="60dp"
android:layout_height="30dp"
android:textAllCaps="false"
android:background="@drawable/curved_border_bgd_red"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:stateListAnimator="@null"
android:text="@string/btn_text_bid"
/>
</RelativeLayout>
这是我在 recyclerview 适配器中的(部分)viewholder 类
static class ViewHolder extends RecyclerView.ViewHolder
{
@BindView(R.id.indicator)
CircularIndicator mCircleIndicator;
@BindView(R.id.vp_photos)
ViewPager mViewPagerPhotoView;
public ViewHolder(View itemView)
{
super(itemView);
ButterKnife.bind(this, itemView);
}
}
在 onBindViewHolder 这就是我所拥有的(就 viewpager 而言):
Item item = mDataSet.get(position);
mImageAdapter = new ImageAdapter(mContext, item.getUrlPhotos());
holder.mViewPagerPhotoView.setAdapter(mImageAdapter);
holder.mViewPagerPhotoView.setOffscreenPageLimit(mImageAdapter.getCount() - 1);
holder.mCircleIndicator.setViewPager(holder.mViewPagerPhotoView);
现在我的 reclerview 有 10 个我从服务器加载的项目。事情是,我注意到当我向下滚动到列表的第 7 项时,一切似乎都很好,但是当我向上滚动查看说我的回收站列表的第 1 项时,在第 1 项的 viewpager 中找到的图像往往消失并(暂时)被占位符图像替换,即查看器完全清空,我暂时拥有占位符;如果该项目在我的屏幕上仍然可见,则从服务器再次加载 viewpager 的图像。我选择 Glide 是因为它以缓存功能而闻名。此外,一个浏览器最多可以有 20 个图像 !!...
有没有人知道如何解决这个问题? 我做错什么了吗?? 任何帮助将不胜感激.....
【问题讨论】:
标签: java android android-recyclerview android-viewpager android-adapter