codeestX / GeekNews

:books:A pure reading App based on Material Design + MVP + RxJava2 + Retrofit + Dagger2 + Realm + Glide
3.49k stars 826 forks source link

干货集中营-福利 #77

Closed Yptake closed 7 years ago

Yptake commented 7 years ago

干货集中营-福利 滑动到最后一页会崩溃~ @Override public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { try { super.onLayoutChildren(recycler, state); } catch (IndexOutOfBoundsException e) { e.printStackTrace(); } } 只是捕捉这个异常来解决 @Override public void onBindViewHolder(final ViewHolder holder, int position) { //存在记录的高度时先Layout再异步加载图片 if (mList.get(holder.getAdapterPosition()).getHeight() > 0) { ViewGroup.LayoutParams layoutParams = holder.ivGirl.getLayoutParams(); layoutParams.height = mList.get(holder.getAdapterPosition()).getHeight(); } //还需要在下载图片之前判断是否NO_POSTITION if (holder.getAdapterPosition() != RecyclerView.NO_POSITION) { Glide.with(mContext).load(mList.get(position).getUrl()).asBitmap().diskCacheStrategy(DiskCacheStrategy.ALL) .into(new SimpleTarget(App.SCREEN_WIDTH / 2, App.SCREEN_WIDTH / 2) { @Override public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) { if (mList.get(holder.getAdapterPosition()).getHeight() <= 0) { int width = resource.getWidth(); int height = resource.getHeight(); int realHeight = (App.SCREEN_WIDTH / 2) * height / width; mList.get(holder.getAdapterPosition()).setHeight(realHeight); ViewGroup.LayoutParams lp = holder.ivGirl.getLayoutParams(); lp.height = realHeight; } holder.ivGirl.setImageBitmap(resource); } }); }