recyclerview创建适配器
//图片按照图片数量显示列数
if (data.getData().getImgs().size() == 1) {
//图片只有一张时显示一列
circledetailImgrecy.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
circledetailImgrecy.setAdapter(new CirclesdetailsimgAdapter(CircleDetailsActivity.this, data.getData().getImgs()));
} else if (data.getData().getImgs().size() == 2 || data.getData().getImgs().size() == 4) {
//图片两张或者四张时显示两列
circledetailImgrecy.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
circledetailImgrecy.setAdapter(new CirclesdetailsimgAdapter(CircleDetailsActivity.this, data.getData().getImgs()));
} else {
//其他数量图片显示三列
circledetailImgrecy.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
circledetailImgrecy.setAdapter(new CirclesdetailsimgAdapter(CircleDetailsActivity.this, data.getData().getImgs()));
}
适配器中
/**
* date:2020/9/1 0001
* author:wsm (Administrator)
* funcation:帖子详情图片
*/
public class CirclesdetailsimgAdapter extends RecyclerView.Adapter<CirclesdetailsimgAdapter.ViewHolder> {
public static final String TAG = "CirclesdimgAdapter";
private Activity mActivity;
private List<String> list;
public CirclesdetailsimgAdapter(Activity activity, List<String> mlist) {
mActivity = activity;
list = mlist;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mActivity).inflate(R.layout.circleimg_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) holder.mImg.getLayoutParams(); //取控件textView当前的布局参数
if (list != null) {
if (list.size() == 1) {
linearParams.height = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, APPApplication.getContext().getResources().getDisplayMetrics()));
holder.mImg.setVisibility(View.VISIBLE);
Glide.with(mActivity).load(list.get(position)).placeholder(R.mipmap.repalec).skipMemoryCache(false).into(holder.mImg);
return;
}
if (list.size() == 2 || list.size() == 3) {
linearParams.height = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, APPApplication.getContext().getResources().getDisplayMetrics()));
holder.mImg.setVisibility(View.VISIBLE);
Glide.with(mActivity).load(list.get(position)).placeholder(R.mipmap.repalec).skipMemoryCache(false).into(holder.mImg);
return;
}
if (position > 9) {
linearParams.height = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 75, APPApplication.getContext().getResources().getDisplayMetrics()));
holder.mImg.setVisibility(View.GONE);
Glide.with(mActivity).load(list.get(position)).placeholder(R.mipmap.repalec).skipMemoryCache(false).into(holder.mImg);
return;
} else {
linearParams.height = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 75, APPApplication.getContext().getResources().getDisplayMetrics()));
Log.v(TAG, "---------list.size==" + list.size() + "---------i==" + position);
holder.mImg.setVisibility(View.VISIBLE);
Glide.with(mActivity).load(list.get(position)).placeholder(R.mipmap.repalec).into(holder.mImg);
}
holder.mImg.setLayoutParams(linearParams);
}
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final RadiuImageView mImg;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mImg = itemView.findViewById(R.id.circledetails_img);
}
}
}
布局文件中设置高自适应,宽填充满即可
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:orientation="vertical">
<.view.RadiuImageView
android:id="@+id/circledetails_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:radius="5dp" />
</LinearLayout>