android - RecyclerView displays network images and Fresco loads them. Under target sdk version 25, the image positions are out of order and multiple images are superimposed.
阿神
阿神 2017-05-24 11:38:04
0
1
1354

Use Recyclerview to display a series of pictures. Use fresco to load.
In order to make imageView adapt to the image aspect ratio, fresco sets DraweeController to calculate the size.

This code can work normally under target Sdk version 17, and the image can be displayed normally. For example, 5 pictures are displayed one by one in order.
Later I set the target sdk version 25. Without changing the code, all five pictures were loaded. However, the positions of the five pictures were superimposed together and only one picture could be seen. I changed to target sdk version 17 and it worked normally again. . .

Please tell me what is going on. After all, android O is out. . . .

Layout file

 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/activity_circle_details"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white"
>
<TextView
    android:id="@+id/test_item_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textSize="17sp"
    android:layout_margin="5dp"
    android:lineSpacingMultiplier="1.5"
    android:textColor="@color/black"
    />
<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/test_item_image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="1dp"
    android:layout_marginRight="1dp"
    android:layout_marginBottom="5dp"
    fresco:placeholderImage="@mipmap/img_empty"
    fresco:placeholderImageScaleType="fitCenter"
    fresco:failureImage="@mipmap/img_error"
    />
</LinearLayout>

The key code called by RecyclerView.Adapter in onBindViewHolder. data is a bean, getImage gets the image url

if(data.getImage()!=null && data.getImage().length()>0) {
            imageView.setVisibility(View.VISIBLE);
            //imageView.setImageURI(data.getImage());
          setControllerListener(imageView, data.getImage(),Tools.getScreenWidth(CircleDetailsActivity.this));

Fresco’s code

/***
 * 根据图片大小,更新view的大小自适应图片,按宽高比缩放
 * 不知道为什么。targetVersion必须为4.2. 如果我设置为7.1则会发生图像覆盖的现象只能看到最后一张图
 * @param simpleDraweeView
 * @param imagePath
 * @param imageWidth
 */
public static void setControllerListener(final SimpleDraweeView simpleDraweeView, String imagePath, final int imageWidth) {
    final ViewGroup.LayoutParams layoutParams = simpleDraweeView.getLayoutParams();
    ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
        @Override
        public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
            if (imageInfo == null) {
                return;
            }
            int height = imageInfo.getHeight();
            int width = imageInfo.getWidth();
            layoutParams.width = imageWidth;
            layoutParams.height = (int) ((float) (imageWidth * height) / (float) width);
            simpleDraweeView.setLayoutParams(layoutParams);
        }

        @Override
        public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
            Log.d("TAG", "Intermediate image received");
        }

        @Override
        public void onFailure(String id, Throwable throwable) {
            throwable.printStackTrace();
        }
    };
   
    DraweeController controller = Fresco.newDraweeControllerBuilder().setControllerListener(controllerListener).setTapToRetryEnabled(true).setUri(Uri.parse(imagePath)).build();

    simpleDraweeView.setController(controller);
}



Package Reference

 compile 'com.android.support:appcompat-v7:25.3.1'
 compile 'com.facebook.fresco:fresco:0.13.0'
 compile 'com.squareup.okhttp3:okhttp:3.7.0'
 compile 'com.facebook.fresco:animated-gif:0.13.0'
 compile 'com.facebook.fresco:imagepipeline-okhttp3:0.13.0+'
 compile 'com.android.support:recyclerview-v7:25.3.1'
 compile 'com.android.support:percent:25.3.1'
 compile 'com.android.support:design:25.3.1'
 compile 'com.android.support:support-v4:25.3.1'
阿神
阿神

闭关修行中......

reply all(1)
曾经蜡笔没有小新

It should be a layout problem. Cannot nest scrollView inside scrollView

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template