我有一个RecyclerView。 recyclerview中的行包含一个ImageView。我正在使用Glide加载图像(大小为30-80 KB /图像)。如果我从上到下滚动,最后会得到OutOfMemoryException。 测试设备相当老:三星Galaxy S3。
我知道有很多类似的问题。我已经尝试了所有方法,但到目前为止还没有成功。
RecyclerView
在ViewPager
中的一个片段中(有2页)。
行的布局:
...
<ImageView
android:id="@+id/catch_pic"
android:layout_width="0dp"
android:layout_height="@dimen/_250sdp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/description"/>
...
我的适配器:
...
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
final Catch actualCatch = mItems.get(position);
CatchViewHolder catchHolder = (CatchViewHolder) holder;
if (actualCatch.getCatchPic() != null && !actualCatch.getCatchPic().isEmpty()) {
catchHolder.mCatchPic.setVisibility(View.VISIBLE);
Glide
.with(catchHolder.itemView.getContext())
.load(BASE_URL_FOR_IMAGES.concat(actualCatch.getCatchPic()))
.apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(30)).skipMemoryCache(true))
.into(catchHolder.mCatchPic);
} else {
catchHolder.mCatchPic.setVisibility(View.GONE);
}
}
@Override
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
super.onViewRecycled(holder);
CatchViewHolder catchHolder = (CatchViewHolder) holder;
Glide.with(holder.itemView.getContext()).clear(catchHolder.mCatchPic);
}
...
Logcat详细信息(滚动时):
I/dalvikvm-heap: Grow heap (frag case) to 14.435MB for 3768016-byte allocation
I/dalvikvm-heap: Grow heap (frag case) to 19.355MB for 1377016-byte allocation
I/dalvikvm-heap: Grow heap (frag case) to 25.652MB for 1632016-byte allocation
I/dalvikvm-heap: Grow heap (frag case) to 38.937MB for 2764816-byte allocation
I/dalvikvm-heap: Grow heap (frag case) to 42.324MB for 2160016-byte allocation
I/dalvikvm-heap: Grow heap (frag case) to 45.533MB for 2186256-byte allocation
I/dalvikvm-heap: Grow heap (frag case) to 42.929MB for 2186256-byte allocation
I/dalvikvm-heap: Grow heap (frag case) to 45.331MB for 1997584-byte allocation
我慢慢发疯。谁能帮助我,错误在哪里?
编辑#1 :
如果我暂时将ViewPager限制为1个片段,则结果是相同的:
02-07 20:07:27.564 25465-25890/net.demo I/dalvikvm-heap: Grow heap (frag case) to 39.165MB for 7990288-byte allocation
02-07 20:07:28.344 25465-25892/net.demo I/dalvikvm-heap: Grow heap (frag case) to 26.725MB for 1228816-byte allocation
02-07 20:07:28.799 25465-25892/net.demo I/dalvikvm-heap: Grow heap (frag case) to 34.354MB for 7990288-byte allocation
02-07 20:07:28.869 25465-25890/net.demo I/dalvikvm-heap: Grow heap (frag case) to 41.971MB for 7990288-byte allocation
02-07 20:07:28.949 25465-25893/net.demo I/dalvikvm-heap: Grow heap (frag case) to 50.640MB for 7990288-byte allocation
02-07 20:07:29.289 25465-25892/net.demo I/dalvikvm-heap: Grow heap (frag case) to 44.330MB for 7990288-byte allocation
02-07 20:07:29.514 25465-25894/net.demo I/dalvikvm-heap: Grow heap (frag case) to 30.028MB for 3048208-byte allocation
02-07 20:07:29.884 25465-25894/net.demo I/dalvikvm-heap: Grow heap (frag case) to 32.937MB for 3048208-byte allocation
02-07 20:07:30.044 25465-25893/net.demo I/dalvikvm-heap: Grow heap (frag case) to 32.940MB for 3048208-byte allocation