RecyclerView与ViewPager [英] RecyclerView vs ViewPager

查看:153
本文介绍了RecyclerView与ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在探索通过 从左向右滑动 从数据库显示数据的选项,并允许用户 添加和删除数据 在数据数组中的任何位置.我发现有两种可能的解决方案可以做到这一点.一个是带有水平滚动的 RecyclerView ,另一个是带有 FragmentStatePagerAdapter的 ViewPager .哪个更有效?就 内存使用情况 易于实施 ?

Currently, I am exploring the option of displaying data from a database by swiping left to right and also allowing users add and remove data from any position in the data array. I found out that there are 2 possible solutions to do this. One is a RecyclerView with horizontal scroll and the other is a ViewPager with a FragmentStatePagerAdapter . Which is more efficient? In terms of Memory usage and Ease of implementation?

谢谢.

推荐答案

我要说的是,它们在内存使用量和易于实现方面具有可比性.它们最大的不同之处在于它们提供给用户的交互.

I would say they are comparable in terms of memory usage and ease of implementation. Where they differ most is in the interaction they provide to the user.

ViewPager 旨在一次显示一项.可见项占据了 ViewPager 的全部宽度.您一次只能滑动一个项目,而滚动始终会捕捉到在中心显示一个项目的情况-您永远不会处于中间位置,只能部分显示两个项目.

ViewPager is designed to show one item at a time. The visible item takes up full width of the ViewPager. You can only swipe one item at a time and scrolling always snaps to showing one item in the centre – you're never left in an in-between position partially showing two items.

RecyclerView 可以具有任意宽度的项目–您可以一次显示很多项目,也可以具有比 RecyclerView 宽的项目的宽度,也可以匹配它们的宽度以模仿 ViewPager .您可以自由滚动-您不仅限于一个项目的宽度或 RecyclerView 的宽度,还可以通过挥动手势来滚动较大的距离.而且没有捕捉-滚动完成时,没有对齐项目到中心或任何一侧.

RecyclerView with a horizontal layout manager on the other hand can have items of any width – you could be showing many items at once or you could have items wider that RecyclerView's width or you could match their widths to mimic ViewPager. You can freely scroll – you are not limited to one item width or RecyclerView's width, you can do a fling gesture to scroll big distances. And there's no snapping – when the scroll finishes there's no aligning items to the centre or any of the sides.

如您所见,存在一些差异.我建议您根据要实现的UI选择窗口小部件.如果您想要 ViewPager 的行为(一次可见一项,请滑动一次以限制一项,然后捕捉以显示完整项),然后使用 ViewPager .使用 RecycleView 复制此行为是可能的,但并非不容易.我绝对会说,如果要使其表现得像 ViewPager 那样,使用 RecyclerView 会更加困难.相反,自定义 ViewPager 的行为几乎是不可能的,因此,如果这不是您想要的,那么您绝对应该使用 RecyclerView .

As you see there are a few differences. I would recommend you to choose your widget based on the UI you want to achieve. If you want ViewPager's behaviour (one item visible at a time, swipe limited to one item and snapping to show the full item) then go with a ViewPager. It's possible but not trivial to replicate this behaviour using a RecycleView. I would definitely say it is way more difficult to use RecyclerView if you want to make it behave like ViewPager. Conversely it's pretty much impossible to customise ViewPager's behaviour, so if that's not what you want then you definitely should use a RecyclerView.

这篇关于RecyclerView与ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆