android - RecyclerView显示网络图片,Fresco加载,在target sdk version 25下图片位置错乱,多张图会叠加
阿神
阿神 2017-05-24 11:38:04
[Android讨论组]

使用Recyclerview展示一系列图片。使用fresco加载。
为了让imageView自适应图片宽高比,所以fresco设置了DraweeController 计算大小。

这段代码在target Sdk version 17的情况下,是能正常工作,图片可以正常显示。比如5张图按顺序一一展示。
后来我设置target sdk version 25,代码未更改的情况下,5张图都有执行加载,但是5张图的位置叠加到一起,只能看到一张图片。我改为target sdk version 17又能正常工作了。。。

求教这是什么情况。毕竟android O都出来了。。。。

布局文件

 
<?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>

RecyclerView.Adapter在onBindViewHolder中调用的关键代码。data是bean,getImage获取图片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的代码

/***
 * 根据图片大小,更新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);
}



包引用

 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'
阿神
阿神

闭关修行中......

全部回复(1)
曾经蜡笔没有小新

应该是布局问题。不能在scrollView中嵌套scrollView

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号