当前位置:网站首页>5.使用RecyclerView优雅的实现瀑布流效果
5.使用RecyclerView优雅的实现瀑布流效果
2022-08-02 14:05:00 【爱上学习啊】
/** * 作者:Pich * 原文链接:http://me.woblog.cn/ * QQ群:129961195 * 微信公众号:woblog * Github:https://github.com/lifengsofts */概述
从前我们想实现一个瀑布流效果是很难得,需要自己自定义控件,可以说是很麻烦,而且性能也好优化,但是现在就不一样了,因为RecyclerView到来了,他可以很方便的实现瀑布流效果。下面就来看看吧,先来一张效果图:

使用StaggeredGridLayoutManager
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,
LinearLayoutCompat.VERTICAL);
rv.setLayoutManager(layoutManager);然后需要注意的就是在Adapter里面需要动态计算图片的高度和宽度。
计算Item的高度
我们这里使用的是Glide图片加载框架,其他图片加载框架也有类似的方法,我们需要做的就是在图片加载回来拿到图片的高度和宽度动态计算Item的高度。
Glide.with(WaterfallFlowActivity.this).load(d).diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable resource,
GlideAnimation<? super GlideDrawable> glideAnimation) {
Log.d("TAG", iv.getWidth() + "," + resource.getIntrinsicWidth());
//计算ImageView的高度
int imageWidth = resource.getIntrinsicWidth();
int imageHeight = resource.getIntrinsicHeight();
int imageViewWidth = iv.getWidth();
double scale = imageWidth * 1.0 / imageViewWidth;
LayoutParams layoutParams = iv.getLayoutParams();
layoutParams.height = (int) (imageHeight / scale);
iv.setLayoutParams(layoutParams);
iv.setImageDrawable(resource);
}
});现在效果其实就已经差不多出来了,但是还有一些优化的细节需要处理。
边栏推荐
猜你喜欢
随机推荐
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十二章)
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十三章)
我理解的学习金字塔
ToF相机从Camera2 API中获取DEPTH16格式深度图
无人驾驶综述:国外国内发展历程
数据乱码问题—更改mysql字符编码
函数递归和动态内存初识
Verilog Learning Series
Kubernetes核心概念
Visual Studio配置OpenCV之后,提示:#include<opencv2/opencv.hpp>无法打开源文件
MySQL知识总结 (四) 事务
MongoDB Compass 安装与使用
Flink前期代码结构
Scala学习总结
Using the cloud GPU + pycharm training model to realize automatic background run programs, save training results, the server automatically power off
The specific operation process of cloud GPU (Hengyuan cloud) training
What's wrong with running yolov5 (1) p, r, map are all 0
C语言字符串——关于指针
宝塔搭建PESCMS-Ticket开源客服工单系统源码实测
Eslint规则大全









