当前位置:网站首页>5. Use RecyclerView to elegantly achieve waterfall effect
5. Use RecyclerView to elegantly achieve waterfall effect
2022-08-02 15:12:00 【Fall in love with learning!】
/** * Author: Pich * Original link: http://me.woblog.cn/ * QQGroup: 129961195 * WeChat public account: woblog * Github: https://github.com/lifengsofts */
Overview
In the past, it was very difficult for us to achieve a waterfall flow effect. We needed to customize the controls ourselves. It can be said to be very troublesome, and the performance is also optimized, but now it is different, because RecyclerView has arrived, he can be very convenient.Achieve a waterfall effect.Let's take a look below, first come to a rendering:
Use StaggeredGridLayoutManager
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,LinearLayoutCompat.VERTICAL);rv.setLayoutManager(layoutManager);
Then it should be noted that the height and width of the image need to be dynamically calculated in the Adapter.
Calculate the height of the item
We use the Glide image loading framework here, and other image loading frameworks have similar methods. What we need to do is to dynamically calculate the height of the Item when the image is loaded back to get the height and width of the image.
Glide.with(WaterfallFlowActivity.this).load(d).diskCacheStrategy(DiskCacheStrategy.ALL).into(new SimpleTarget() {@Overridepublic void onResourceReady(GlideDrawable resource,GlideAnimation super GlideDrawable> glideAnimation) {Log.d("TAG", iv.getWidth() + "," + resource.getIntrinsicWidth());//Calculate the height of ImageViewint 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);}});
The effect is almost finished now, but there are still some optimization details to be dealt with.
边栏推荐
- FP6195耐压60V电流降压3.3V5V模块供电方案
- HAL框架
- 神经网络可以解决一切问题吗:一场知乎辩论的整理
- 2.4G无线小模块CI24R1超低成本
- FP5139电池与适配器供电DC-DC隔离升降压电路反激电路电荷泵电路原理图
- Cannot figure out how to save this field into database. You can consider adding a type converter for
- 镜像法求解接地导体空腔电势分布问题
- 文本匹配任务
- Manifest merger failed with multiple errors, see logs
- PyTorch②---transforms结构及用法
猜你喜欢
随机推荐
LLVM系列第十章:控制流语句if-else-phi
Policy Evaluation收敛性、炼丹与数学家
NER(命名体识别)之 FLAT模型
可以拖拽的ViewGroup,仿微信拖拽缩放关闭
国内IT市场还有发展吗?有哪些创新好用的IT运维工具可以推荐?
LLVM系列第十九章:写一个简单的Module Pass
ARMv8虚拟化
循环神经网络RNN 之 LSTM
LLVM系列第九章:控制流语句if-else
“非图灵完备”到底意味着什么
对疫情期间量化策略表现的看法
BLE蓝牙5.2-PHY6222系统级芯片(SoC)智能手表/手环
vscode compiles the keil project and burns the program
GICv3/v4-软件概述
DP1332E内置c8051的mcu内核NFC刷卡芯片国产兼容NXP
Detailed explanation of RecyclerView series article directory
PyTorch⑤---卷积神经网络_卷积层
Bert系列之 Transformer详解
让深度学习歇一会吧
Manifest merger failed with multiple errors, see logs