当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
“自主可控”的正确姿势
牛客刷题汇总(持续更新中)
ARMv8虚拟化
checkPermissions Missing write access to /usr/local/lib
Visual studio代码中有红色波浪线解决办法
ConstraintLayout从入门到放弃
2.4G无线小模块CI24R1超低成本
PyTorch⑨---卷积神经网络_线性层
STM32F1和F4的区别
FP7195芯片PWM转模拟调光至0.1%低亮度时恒流一致性的控制原理
【深度学习中的损失函数整理与总结】
LORA芯片ASR6505无线远距离传输8位MCU
AAPT: error: duplicate value for resource ‘attr/xxx‘ with config ‘‘, file failed to compile.
【我的电赛日记(三)】STM32学习笔记与要点总结
PyTorch⑦---卷积神经网络_非线性激活
使用flutter小记
“非图灵完备”到底意味着什么
让深度学习歇一会吧
神经网络可以解决一切问题吗:一场知乎辩论的整理
LLVM系列第二十五章:简单统计一下LLVM源码行数