Android TV Leanback加载图标图像以减少内存占用的最佳位置 [英] Android Tv Leanback best place to load icon images to reduce memory footprint

查看:152
本文介绍了Android TV Leanback加载图标图像以减少内存占用的最佳位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Leanback制作了一个用于Android电视的应用程序,我正在使用BrowseSupportFragment,并且我的商品由实现Presenter类的CardPresenter类呈现.我正在onBindViewHolder方法中为图标加载图像.但是它将太多的图像加载到内存中,我通过Logcat确认加载的图像总数太多(基本上加载了54行,每行有10-12张卡).这导致应用程序中的gc暂停.我想要的是仅加载可见图像.尝试使用Presenter类中的onViewAttachedToWindow方法,但是方法onBindViewHolder&onViewAttachedToWindow似乎被调用了相同的次数.我想做的就是懒惰地加载图像,当滚动列表行时,尝试搜索很多但无法获得正确的答案,有人可以帮我吗?

I have made an app for Android tv using Leanback i am using the BrowseSupportFragment and my items are rendered by class CardPresenter which implements Presenter class. I am loading images in onBindViewHolder method for the icons. But its loading too many images into memory, i checked by Logcat that the total images loaded are too many(basically around 54 rows are loaded and each row has 10-12 cards).This is causing gc pauses in application. What i want is to load only the visible images. tried using onViewAttachedToWindow method from Presenter class, But the method onBindViewHolder & onViewAttachedToWindow seems be to be called same number of times. What i want to do is to load images lazily, when the listrows are scrolled, tried searching a lot but couldn't get proper answer, Can someone help me with that?

推荐答案

onBindViewHolder是触发图像加载的正确位置.当布局膨胀时,您应该看到onBindViewHolder调用了所有可见项,有时根据截止值为下一个项提供了缓冲区.如果您看到的是在onBindViewHolder上被调用的次数远远超过了有意义的次数(例如,您在屏幕上看到了20个项目,但被调用了100次),那么您应该确保布局具有设定的大小.

onBindViewHolder is the correct place to trigger the image loading. When your layout is inflated, you should see onBindViewHolder called for all visible items, sometimes with a buffer for the next item depending on the cutoff. If what you're seeing is onBindViewHolder being called substantially more times than makes sense (e.g., you see 20 items on screen but it's called 100 times), then you should make sure that your layouts have a set size.

例如,如果要显示卡片,并且直到加载图像后才设置卡片的宽度,则宽度实际上可能为0,因此该行中的每张卡片都被绑定,从而触发了图像请求.ImageCardView具有setMainImageDimensions帮助程序方法来设置LayoutParams,因此您可以在onCreateViewHolder(如果所有项目的大小均相同)或onBindViewHolder(如果大小取决于项目)中调用它.

For example, if you're displaying cards and the width of a card isn't set until after the image is loaded, your width may effectively be 0 so every card in that row is bound, triggering your image requests. ImageCardView has the setMainImageDimensions helper method to set the LayoutParams, so you can call that either in onCreateViewHolder (if the size is the same for all items) or in onBindViewHolder (if the size depends on the item).

这篇关于Android TV Leanback加载图标图像以减少内存占用的最佳位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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