适用于Android上不同孩子的ScrollView与RecyclerView [英] ScrollView vs RecyclerView for dissimilar children on Android

查看:140
本文介绍了适用于Android上不同孩子的ScrollView与RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我混合使用10-15个自定义视图和片段,以垂直列表的形式显示它们.我不确定在所有视图都不相同的情况下RecyclerView是否具有任何优势. RecyclerView似乎增加了很多样板代码,而且我认为我获得的唯一好处就是更容易进入/退出动画.

I have a mix of 10-15 custom views and fragments to be shown in a vertical list. I am not sure if RecyclerView has any advantage in scenarios where all views are dissimilar. RecyclerView seems to add lot of boiler-plate code, and I think the only advantage I would get is easier enter/exit animation.

我的自定义视图/片段也正在创建时调用Web服务.出于业务原因,我们不缓存网络请求.我的理解是,RecyclerView将在每次绑定时触发这些Web服务调用,从而导致冗余调用和可见延迟.相对而言,ScrollView应该只加载一次视图,并将其全部保存在内存中,从而避免多次调用.

My custom views/fragment also make web-service call on being created. We don't cache web-requests for business reasons. My understanding is that RecyclerView would trigger these web-service calls on each binding, resulting in redundant calls and visible latency. Comparatively ScrollView should load the views once, and it keeps them all in memory, avoiding multiple calls.

我的理解正确吗?在给定场景下,我需要一些帮助来理解ScrollViews对性能的影响.

Is my understanding correct ? I need some help understanding performance implications with ScrollViews, in the given scenario.

推荐答案

ScrollView

使用ScrollView,将立即创建其所有子视图,而不管屏幕上的可见性如何.如果将ScrollView用于您的解决方案,则您可能希望先使用占位符侦听"其子视图何时可见以更新其内容.您还可以构建一些将在后台线程中获取内容的东西.这可能会变得比您想的要复杂得多.

With a ScrollView, all of its subviews will be created at once, regardless of visibility on screen. If using a ScrollView for your solution, you'll probably want to "listen" for when its subviews become visible to update their content, using placeholders initially. You could also build something that will fetch the content in a background thread. This may get more complex than you want very quickly.

RecyclerView

A RecyclerView的优点是将子视图的创建推迟到自动可见时才可以创建,并且可以重新使用具有常见布局的子视图.

A RecyclerView provides the advantage of deferring creation of child views until they become visible automatically, and can re-use child views with common layouts.

通过为每个孩子使用不同的项目视图类型",您将禁用RecyclerView的回收"部分,但是仍然受益于将视图的创建推迟到滚动到视图中为止.

By using different "item view types" for each of your children, you'll disable the "recycling" part of RecyclerView, but still get the benefit of deferring the creation of views until they are scrolled into view.

RecyclerViews确实为您提供了一种结构化的模式,供您通过Adapter和ViewHolders使用.尽管对RecyclerView并不熟悉,但它还具有一个RecyclerView.ViewCacheExtension,旨在使开发人员可以控制视图的缓存.

RecyclerViews do provide a fairly structured pattern for you to work with via the Adapter and ViewHolders. Though not personally familiar with it, RecyclerView also has a RecyclerView.ViewCacheExtension which is intended to give the developer control over caching of views.

总体而言,后期绑定(不创建和加载可能永远不会查看的视图)的优点以及RecyclerView的灵活性可能会为您带来良好的结果.

Overall, the advantage of late binding (don't create and load views that might never be viewed) and the flexibility of the RecyclerView will probably yield good results for you.

这篇关于适用于Android上不同孩子的ScrollView与RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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