当前位置:贵波网 > 生活 > 正文

recyclerview瀑布流怎么控制item大小

2020-08-07 15

adapter里面计算宽高,宽高可以让服务器获让我们以自己获取。
下面就说现方式吧
既然要动态适配宽高就要根据图片的宽度和手机的宽度计算出比率来然后根据这个比率来计算imageview的高度
package com.jtech.scrollimageloaddemo;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jtech.adapter.RecyclerAdapter;
import com.jtech.view.RecyclerHolder;

import java.util.ArrayList;
import java.util.List;

/**
* 图片适配器
* 关于 同等间距的recycleView
* 我们在xml文件里设置的距离是dp 在代码里设置的距离是px
* 所以都在代码中设置统一格式就是同等编剧的recyclerview
* Created by wuxubaiyang on 16/5/6.
*/
public class ImageAdapter extends RecyclerAdapter<ImageModel> {

private boolean isScroll = false;
private int itemWidth;
public ImageAdapter(Activity activity) {
super(activity);
//计算item的宽度
itemWidth = (DeviceUtils.getScreenWidth(activity)-48) / 2;
}

public void setScroll(boolean scroll) {
isScroll = scroll;
if (!isScroll) {
notifyDataSetChanged();
}
}

@Override
public View createView(LayoutInflater layoutInflater, ViewGroup viewGroup, int i) {
return layoutInflater.inflate(R.layout.view_item, viewGroup, false);
}

@Override
public void convert(RecyclerHolder recyclerHolder, ImageModel imageModel, int i) {
ImageView imageView = recyclerHolder.getView(R.id.imageview);
//等比缩放
double ratio = (itemWidth * 1.0) / imageModel.getWidth();
int height = (int) (imageModel.getHeight() * ratio);
ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.width = itemWidth;
layoutParams.height = height;
imageView.setLayoutParams(layoutParams);
//显示图片
// if (isScroll) {
// imageView.setImageResource(R.mipmap.ic_launcher);
// } else {
Glide.with(getActivity()).load(imageModel.getUrl()).placeholder(R.mipmap.ic_launcher).into(imageView);
// }
}
}
本周热门
热门文章
热门关注