下面就说下实现方式吧
既然要动态适配宽高就要根据的宽度和手机的宽度计算出比率来然后根据这个比率来计算imageview的高度
package comjtechscrollimageloaddemo;
import androidappActivity;
import androidcontentContext;
import androidviewLayoutInflater;
import androidviewView;adapter里面计算宽高,这个宽高可以让服务器获取当让我们也可以自己获取。
下面就说下实现方式吧
既然要动态适配宽高就要根据的宽度和手机的宽度计算出比率来然后根据这个比率来计算imageview的高度
package comjtechscrollimageloaddemo;
import androidappActivity;
import androidcontentContext;
import androidviewLayoutInflater;
import androidviewView;
import androidviewViewGroup;
import androidwidgetBaseAdapter;
import androidwidgetImageView;
import combumptechglideGlide;
import comjtechadapterRecyclerAdapter;
import comjtechviewRecyclerHolder;
import javautilArrayList;
import javautilList;
/
适配器
关于 同等间距的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 = (DeviceUtilsgetScreenWidth(activity)-48) / 2;
}
public void setScroll(boolean scroll) {
isScroll = scroll;
if (!isScroll) {
notifyDataSetChanged();
}
}
@Override
public View createView(LayoutInflater layoutInflater, ViewGroup viewGroup, int i) {
return layoutInflaterinflate(Rlayoutview_item, viewGroup, false);
}
@Override
public void convert(RecyclerHolder recyclerHolder, ImageModel imageModel, int i) {
ImageView imageView = recyclerHoldergetView(Ridimageview);
//等比缩放
double ratio = (itemWidth 10) / imageModelgetWidth();
int height = (int) (imageModelgetHeight() ratio);
ViewGroupLayoutParams layoutParams = imageViewgetLayoutParams();
layoutParamswidth = itemWidth;
layoutParamsheight = height;
imageViewsetLayoutParams(layoutParams);
//显示
// if (isScroll) {
// imageViewsetImageResource(Rmipmapic_launcher);
// } else {
Glidewith(getActivity())load(imageModelgetUrl())placeholder(Rmipmapic_launcher)into(imageView);
// }
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)