recyclerview实现瀑布流图片自适应高度

在这里插入图片描述
1::Recyclerview配置适配器

  StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
       CircleAdapter mCircleAdapter = new CircleAdapter(getActivity());
        recyclerview.setLayoutManager(gridLayoutManager);
        recyclerview.setAdapter(mCircleAdapter);

2:自定义view实现高度自适应

/**
 * date:2020/9/8 0008
 * author:wsm (Administrator)
 * funcation:实现图片高度自适应
 */
public class ResizableImageView extends ImageView {
    
    

    public ResizableImageView(Context context) {
    
    
        super(context);
    }

    public ResizableImageView(Context context, AttributeSet attrs) {
    
    
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    
    
        Drawable d = getDrawable();

        if(d!=null){
    
    
            // ceil not round - avoid thin vertical gaps along the left/right edges
            int width = MeasureSpec.getSize(widthMeasureSpec);
            //高度根据使得图片的宽度充满屏幕计算而得
            int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
            setMeasuredDimension(width, height);
        }else{
    
    
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

}

3:布局使用

<?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:orientation="vertical">

     <。。。.view.ResizableImageView
        android:id="@+id/list_img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY" />
 </LinearLayout>

Glide.with(mActivity).load(mList.get(position).getImgs()).into(holder.mRecomdimg);

图片实现圆角

效果就实现了

猜你喜欢

转载自blog.csdn.net/weixin_43117800/article/details/108321084
今日推荐