在recycleview的adapter中 定义了一组view
Adapter 中的实现如下:
gholder.sdv_item_person_pic1.setVisibility(View.GONE);
gholder.sdv_item_person_pic2.setVisibility(View.GONE);
gholder.sdv_item_person_pic3.setVisibility(View.GONE);
for (int i = 0; i < size; i++) {
OrderItem.ServeListBean serveListBean = serveList.get(i);
// LogUtil.i("pic..." + pic);
switch (i) {
case 0:
if (serveListBean != null) {
gholder.sdv_item_person_pic1.setVisibility(View.VISIBLE);
}
if (!TextUtils.isEmpty(serveListBean.getProductImg())) {
gholder.sdv_item_person_pic1.setImageURI(Uri.parse(serveListBean.getProductImg()));
}
break;
case 1:
if (serveListBean != null) {
gholder.sdv_item_person_pic2.setVisibility(View.VISIBLE);
}
if (!TextUtils.isEmpty(serveListBean.getProductImg())) {
gholder.sdv_item_person_pic2.setImageURI(Uri.parse(serveListBean.getProductImg()));
}
break;
case 2:
if (serveListBean != null) {
gholder.sdv_item_person_pic3.setVisibility(View.VISIBLE);
}
if (!TextUtils.isEmpty(serveListBean.getProductImg())) {
gholder.sdv_item_person_pic3.setImageURI(Uri.parse(serveListBean.getProductImg()));
}
break;
}
}
}
虽然初始化为不显示,并正确判断了url非空显示,但是还是有图片的错位参数
下面为修改正确的结果:
ArrayList piclist = new ArrayList<>();
for (int i = 0; i < size; i++) {
if (serveList.get(i) != null) {
piclist.add(serveList.get(i).getProductImg());
} else {
piclist.add("");
}
}
holder.rlv_threesdv.setAdapter(new SdvAdapter(mcontext, piclist));
SdvAdapter中的onBindViewHolder
onBindViewHolder
public void onBindViewHolder(final SdvAdapterViewHoder holder, int position) {
if (!Tools.isNull(list.get(position) + ""))
holder.sdv.setImageURI(Uri.parse(list.get(position) + ""));
}
用一个新的recycleview放在adapter的itemView中处理了图片错位的问题
有两种方法 第一种是设置固定宽高比例扩充 另一种是通过宽度来计算高度
1、只有希望显示固定的宽高比时,可以使用wrap_content。
如果希望图片以特定的宽高比例显示,例如 4:3,可以在XML中指定:
在代码中指定宽高比例
mSimpleDraweeView.setAspectRatio(1.33f);
2、宽度来计算高度 可以通过监听下载事件实现
监听下载事件:
ControllerListener controllerListener = new BaseControllerListener() {
@Override
public void onFinalImageSet(
String id,
@Nullable ImageInfo imageInfo,
@Nullable Animatable anim) {
if (imageInfo == null) {
return;
}
QualityInfo qualityInfo = imageInfo.getQualityInfo();
FLog.d("Final image received! " +
"Size %d x %d",
"Quality level %d, good enough: %s, full quality: %s",
imageInfo.getWidth(),
imageInfo.getHeight(),
qualityInfo.getQuality(),
qualityInfo.isOfGoodEnoughQuality(),
qualityInfo.isOfFullQuality());
}
@Override
public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
FLog.d("Intermediate image received");
}
@Override
public void onFailure(String id, Throwable throwable) {
FLog.e(getClass(), throwable, "Error loading %s", id)
}
};
Uri uri;
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setControllerListener(controllerListener)
.setUri(uri)
// other setters
.build();
mSimpleDraweeView.setController(controller);
修改的方法:
/**
* 通过imageWidth 的宽度,自动适应高度
* * @param simpleDraweeView view
* * @param imagePath Uri
* * @param imageWidth width
*/
public static void setControllerListener(final SimpleDraweeView simpleDraweeView, String imagePath, final int imageWidth) {
final ViewGroup.LayoutParams layoutParams = simpleDraweeView.getLayoutParams();
ControllerListener controllerListener = new BaseControllerListener() {
@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).setUri(Uri.parse(imagePath)).build();
simpleDraweeView.setController(controller);
}
我在RecycleViewAdapter的实现
ScreenUtil:
public static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
@Override
public void onBindViewHolder(SimpleImageHolder holder, int position) {
if (!Tools.isNull(list.get(position))) {
// holder.sdv_simple.setImageURI(Uri.parse(list.get(position)));
Tools.setControllerListener(holder.sdv_simple, list.get(position),
ScreenUtil.getScreenWidth(context));
}
}