recyclerview瀑布流怎么控制item大小
1个回答
2016-09-05 · 知道合伙人软件行家
关注
展开全部
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);
// }
}
}
下面就说下实现方式吧
既然要动态适配宽高就要根据图片的宽度和手机的宽度计算出比率来然后根据这个比率来计算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);
// }
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询