RecyclerView-对网格布局管理器列进行动画更改 [英] RecyclerView - animate change of grid layout manager columns
问题描述
我想为RecyclerView
s GridLayoutManager
的更改添加动画效果.默认情况下,我在3列的网格中显示项目列表,用户可以选择显示更多或更少的列.
I want to animate the change of my RecyclerView
s GridLayoutManager
. I defaulty show a list of items in a grid with 3 columns and the user can select to show more or less columns.
我希望RecyclerView
中的views
可以移动/缩放到新位置,但是我不知道该怎么做.
I would like the views
in the RecyclerView
to move/scale to their new positions, but I have no idea how this could be done.
我到底想要什么
- 允许通过展开/收缩触摸手势缩放网格=>我知道该怎么做
- 为
LayoutManager
的更改添加动画效果
- allow to scale the grid via an expand/contract touch gesture => I know how to do that
- animate the change of the
LayoutManager
有人知道我如何为LayoutManager
的变化制作动画吗?
Does anyone know how I can animate the change of the LayoutManager
?
推荐答案
此处的灵感来源将是Google相册应用,股票索尼图库应用
The source of inspiration here would be the Google Photos app,the stock Sony Gallery app
基本上可以使用两种方法:
There are basically 2 approaches you can go with:
-
您可以使用 setSpanCount(int)
您使用 SpanSizeLookUp 可以即时更改每个项目的spanSize.
You set a very high span count(~100) use the SpanSizeLookUp to change the per item spanSize on the fly.
- 我使用了Musenkishi提供的要点,用于示例GitHub项目中使用了这种方法.
- 注意事项:
- 我目前使用点击侦听器来不断修改跨度大小查找.可以将其更改为ItemGestureListener来捕获收缩缩放事件并进行相应的更改.
- 您需要确定一种选择跨度计数的方法,以使一行中的所有项目都占据整个屏幕宽度(因此您看不到任何空白空间)
- 由于无法从bindView/createView等内部调用notifyChanged方法,因此您使用延迟的可运行帖子来调用notifyItemRangeChanged.
- 更改跨度大小后,您需要 notifyItemRangeChanged 设置在适当的范围内,以使当前显示在屏幕上的所有项目都进行相应的移动.我已经使用了(底部的代码)
- I have used the Gist provided by Musenkisfor this answer to provide an animator to animate the changes in grid layout changes
- I have used this approach in a sample GitHub project implementing the same.
- Caveats:
- I have currently used the click listener to keep modifying the the span size look up.This could be changed to a ItemGestureListener to capture pinch zoom events and change accordingly.
- You need to determine a way to choose a span count so that all the items in a row occupy the entire screen width (and hence you do not see any empty space)
- You call notifyItemRangeChanged using a runnable post delayed since you cannot call the notifyChanged methods from within bindView/createView etc.
- After changing the span size,you need to notifyItemRangeChanged with an appropriate range so that all the items currently displayed on the screen are shifted accordingly.I have used (code at the bottom)
这不是一个完整的解决方案,而是一个2小时的解决方案.显然,您可以在提到的所有问题上进行改进:). 我希望继续更新样本,因为这种观点一直让我着迷. 不要将其视为最终解决方案,而只是实现此方法的一种特殊方式.如果要改用StaggerredLayoutManager,则可以轻松避免项目之间的空白.
This is not a complete solution but a 2 hour solution for the same.You can obviously improve on all the points mentioned :). I hope to keep updating the sample since this kind of views have always fascinated me. Do not view this as the final solution but just a particular way of achieving this approach. If you were to use a StaggerredLayoutManager instead,you could easily avoid blank spaces between items.
public int calculateRange() { int start = ((GridLayoutManager) grv.getLayoutManager()).findFirstVisibleItemPosition(); int end = ((GridLayoutManager) grv.getLayoutManager()).findLastVisibleItemPosition(); if (start < 0) start = 0; if (end < 0) end = getItemCount(); return end - start; }
这篇关于RecyclerView-对网格布局管理器列进行动画更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!