RecyclerView 瀑布流 StaggeredGridLayoutManager 图片自适应高度

 

 

效果图

 

不废话直接上代码

1.首先要集成  glide

implementation 'com.github.bumptech.glide:glide:4.12.0'

 

2.在主页(  没有什么特殊操作, 就是实例化适配器)

 

StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
rc_listview.setLayoutManager(staggeredGridLayoutManager);

adapter_home_recoment = new Adapter_Home_Recoment(BaseApplication.getContext(), datalist);
rc_listview.setAdapter(adapter_home_recoment);

 

3. 在适配器内

首先本地拿到图片的宽高
//获图片宽高,
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(compress_path, options);
 options.outHeight;//高
options.outWidth;//宽

把图片的宽高拼接到图片的名称内, 上传到服务器, 以上步骤需要在上传图片时完成!!!!!(因为适配器里要用到图片的原始宽高)

(拼接思路,如果服务器对图片链接做了处理, 返回了重新编译后的路径, 
则在 总的提交步骤时, 对服务器返回的 图片路径经行拼接, 
 如:http://11111.jpg?高+"&"+宽,http://11111.jpg  是服务器返回的上传后的路径)




然后,在适配器的 onBindViewHolder内做如下操作
ViewGroup.LayoutParams layoutParams = holder.ig_goodpic.getLayoutParams();
float itemWidth = (Utils.getScreenWidth(context) -  3) / 2;//每个item 的宽, 我这里是-3,是适配间距问题
layoutParams.width = (int) itemWidth;
float scale = (itemWidth + 0f) / 宽;//从链接中截取的
layoutParams.height = (int) ((Integer.parseInt(高)) * scale);//从链接中截取的
holder.ig_goodpic.setLayoutParams(layoutParams);




//图片加载高度自适应
Glide.with(context).
        load(datalist.get(position).getCover()).
        placeholder(R.drawable.img_big_loading).
        override(layoutParams.width, layoutParams.height).//重写图片的宽高,计算后的
        into(holder.ig_goodpic);

 

 

工具类:
public static int getScreenWidth(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.widthPixels;
}




//防止第一行空白
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void  onScrollStateChanged(RecyclerView recyclerView, int newState) {
        //防止第一行到顶部有空白区域
        layoutManager.invalidateSpanAssignments();
    }
});

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要让 RecyclerView高度随着子项自适应,可以采用以下两种方法: 方法一:使用 WrapContentLinearLayoutManager WrapContentLinearLayoutManagerRecyclerView 的一个布局管理器,可以让 RecyclerView高度随着子项自适应。 首先,需要在项目的 build.gradle 中添加以下依赖: ``` implementation 'com.github.azhao1981:WrapContentLinearLayoutManager:v1.0.1' ``` 接着,在代码中设置 RecyclerView 的布局管理器为 WrapContentLinearLayoutManager,如下: ```java WrapContentLinearLayoutManager layoutManager = new WrapContentLinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); ``` 方法二:手动计算高度 另一种方法是手动计算 RecyclerView高度,具体步骤如下: 1. 在 RecyclerView 的 Adapter 中,添加一个方法用于计算 RecyclerView高度: ```java public int getItemHeight() { int height = 0; for (int i = 0; i < getItemCount(); i++) { View view = LayoutInflater.from(mContext).inflate(R.layout.item_layout, null); view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); height += view.getMeasuredHeight(); } return height; } ``` 2. 在 Activity 或 Fragment 中获取 RecyclerView高度,并设置给 RecyclerView: ```java int height = mAdapter.getItemHeight(); ViewGroup.LayoutParams layoutParams = recyclerView.getLayoutParams(); layoutParams.height = height; recyclerView.setLayoutParams(layoutParams); ``` 其中,getItemHeight() 方法中的 R.layout.item_layoutRecyclerView 的子项布局文件。如果 RecyclerView 的子项布局文件中包含了多个子 View,请将它们的高度相加。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值