壁画:滚动RecylcerView

时间:2016-01-10 23:01:09

标签: android android-recyclerview fresco

我有一个Horizo​​ntal RecyclerView:每个项目都使用Facebook Fresco图像库加载了一个图像。但是,当正确的图像在屏幕上稍微滚动时会加载正确的图像,但当RecyclerView进一步滚动时它会消失。

此外,还存在来回滚动将错误图像放入错误项目的问题(我认为这是回收视图的问题)。也就是说,您可以看到覆盖下面屏幕截图中的项目的文本始终一致地显示在正确的项目上,因此此回收问题仅适用于图像。

我可以确认每次都将SimpleDraweeView传递给正确的URL。

以下是一些屏幕和随附的日志:

screenshot one, showing the image entering from the right

屏幕截图1:显示从右侧输入的正确图像。

记录,因为它滚动到屏幕上并且图像已成功加载:



AbstractDraweeController: controller cbb6b5b 5: onTouchEvent MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=32.860107, y[0]=198.16406, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=464189, downTime=464189, deviceId=0, source=0x1002 }
AbstractDraweeController: controller c077610 8: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@d36ab12
AbstractDraweeController: controller 87baf9c null -> 9: initialize
AbstractDraweeController: controller c077610 8: setHierarchy: null
AbstractDraweeController: controller 87baf9c 9: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@d36ab12
AbstractDraweeController: controller 87baf9c 9: onAttach: request needs submit
PipelineDraweeController: controller 87baf9c: getDataSource
AbstractDraweeController: controller 87baf9c 9: submitRequest: dataSource: ad37da5
BufferedDiskCache: Did not find image for http://i.imgur.com/OVwFM9ub.jpg in staging area
BufferedDiskCache: Disk cache read for http://i.imgur.com/OVwFM9ub.jpg
BufferedDiskCache: Found entry in disk cache for http://i.imgur.com/OVwFM9ub.jpg
NativeMemoryChunkPool: Used = (7, 188416); Free = (0, 0)
NativeMemoryChunkPool: get (alloc) (object, size) = (ef2f57a, 16384)
GenericByteArrayPool: Used = (1, 16384); Free = (0, 0)
GenericByteArrayPool: get (reuse) (object, size) = (cf34727, 16384)
GenericByteArrayPool: release (reuse) (object, size) = (cf34727, 16384)
GenericByteArrayPool: Used = (0, 0); Free = (1, 16384)
BufferedDiskCache: Successful read from disk cache for http://i.imgur.com/OVwFM9ub.jpg
TiffUtil: Unsupported orientation
BitmapPool: Used = (7, 2831360); Free = (0, 0)
BitmapPool: get (alloc) (object, size) = (8622988, 102400)
AbstractDraweeController: controller 87baf9c 9: set_final_result @ onNewResult: image: CloseableReference 1a34f21




enter image description here

屏幕截图2:从右向左滚动后显示RecyclerView。这表明先前加载到ViewHolder中的图像现已消失。

屏幕截图1和2之间生成以下日志:



AbstractDraweeController: controller cbb6b5b 5: onTouchEvent MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=103.65283, y[0]=144.14063, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=793409, downTime=793409, deviceId=0, source=0x1002 }
AbstractDraweeController: controller 87baf9c 9: onDetach
AbstractDraweeController: controller e14aa9d 2: onDetach
AbstractDraweeController: controller 87baf9c 9: release: image: CloseableReference 1a34f21
AbstractDraweeController: controller e14aa9d 2: release: image: CloseableReference 61f0be9




代码: ViewHolder XML



<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="120dp"
    android:layout_height="match_parent"
    android:background="#993366">

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/recycler_drawee_view"
        android:layout_width="110dp"
        android:layout_height="match_parent"
        android:background="#336699"
        fresco:placeholderImage="@drawable/placeholder" />

    <TextView
        android:id="@+id/recycler_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEST" />

</FrameLayout>
&#13;
&#13;
&#13;

代码:SimpleDraweeView设置

&#13;
&#13;
GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());
mHierarchy = builder
       .setFadeDuration(300)
       .setPlaceholderImage(context.getDrawable(R.drawable.placeholder))
       .build();

...

mDraweeView.setHierarchy(mHierarchy);
Uri uri = Uri.parse(imageLink);
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(uri)
.build();
mDraweeView.setController(controller);
&#13;
&#13;
&#13;

代码:RecyclerView.Adapter

&#13;
&#13;
@Override
public SimpleHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_view_item, viewGroup, false);
return new SimpleHolder(view, mHierarchy);
}

...

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
holder.setText(EntryUtils.getFullCaption(mData.get(position)));
holder.setImage(EntryUtils.getThumbnailLink(mData.get(position)));
}
&#13;
&#13;
&#13; 感谢您的时间,如果你到目前为止。如果进一步的细节可能有用,请告诉我,我会尝试提供它们。

再次感谢。

1 个答案:

答案 0 :(得分:2)

从logcat中可以看到您正在使用与多个控制器相同的DraweeHierarchy实例。不要那样做。

AbstractDraweeController: controller c077610 8: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@d36ab12
AbstractDraweeController: controller 87baf9c null -> 9: initialize
AbstractDraweeController: controller c077610 8: setHierarchy: null
AbstractDraweeController: controller 87baf9c 9: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@d36ab12

documentation中所述,请勿重复使用DraweeHierarchies。每个DraweeView都需要拥有自己的DraweeHierarchy实例。不要将层次结构存储在mHierarchy中,只需将层次结构设置为视图。

此外,每个视图都需要自己的层次结构,但这并不意味着每次要设置新图像时都必须构建新的层次结构。在创建视图时,您只构建一次层次结构。稍后当您需要设置/更改图像时,只需将新控制器设置为视图。

从您的示例看,您似乎根本不需要以编程方式构建层次结构。您可以在XML中指定淡入淡出持续时间和占位符。顺便说一下,淡入淡出持续时间的默认值是300毫秒,所以你实际上也可以省略该部分。 因此,只需删除构建和设置层次结构的代码(mDraweeView.setHierarchy(mHierarchy))。

如果您只使用Uri而没有任何其他选项,则无需构建控制器。只需mDraweeView.setImageURI(uri)SimpleDraweeView即可为您创建控制器。