当前位置:网站首页>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);
}
});现在效果其实就已经差不多出来了,但是还有一些优化的细节需要处理。
边栏推荐
猜你喜欢

重新学习编程day1 【初始c语言】【c语言编写出计算两个数之和的代码】

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

每周招聘|PostgreSQL专家,年薪60+,高能力高薪资

spark(standalone,yarn)

C语言一级指针(补)

字符串的小知识

MongoDB安装流程心得:

无人驾驶综述:等级划分

MarkDown syntax summary
随机推荐
Flask-RESTful request response and SQLAlchemy foundation
MySQL知识总结 (一) 数据类型
鼠标右键菜单栏太长如何减少
什么是闭包?闭包的作用?闭包的应用?有什么缺点?
MySQL知识总结 (三) 索引
基于ThinkPHP6.0 - 宝塔搭建漫画CMS管理系统源码实测
[ROS] (05) ROS Communication - Node, Nodes & Master
【c】小游戏---扫雷雏形
Eslint规则大全
每周招聘|PostgreSQL专家,年薪60+,高能力高薪资
C语言——一级指针初识
Creating seven NiuYun Flask project complete and let cloud
Using the cloud GPU + pycharm training model to realize automatic background run programs, save training results, the server automatically power off
主存储器(二)
PHP open source customer service system _ online customer service source code PHP
C语言初级—从键盘接收一个整形并逆序输出
Flink实现Exactly Once
MySQL 8.0 新特性
C语言sizeof和strlen的区别
Flask framework in-depth two