ViewPager setCurrentItem冻结UI线程 [英] ViewPager setCurrentItem freezes UI thread

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

问题描述

我正在使用Android支持v13中的ViewPager,我想使用setCurrentItem(int)滚动到特定项目,但是当我尝试滚动2页以上时,应用程序冻结,几秒钟后,系统显示一个ANR窗口.

I am using a ViewPager from Android support v13 and I'd like to scroll to a specific item using setCurrentItem(int), but when I try to scroll more than 2 pages the application freezes, and after a few seconds the system shows an ANR window.

我尝试使用setOffscreenPageLimit(2)增加屏幕外屏幕限制,这样当我尝试滚动2页时它不会冻结,但对3页却没有作用.

I tried to increase the offscreen screen limit using setOffscreenPageLimit(2), that way it did not freeze when i tried to scroll 2 pages, but did the same for 3 pages.

我的问题是我的片段占用大量内存,因此我不想有太多内存.我在支持库v4中使用了相同的代码,但是我不得不将其更新为v13以使用NotificationCompat.Builder.addAction(int, CharSequence, PendingIntent).

My problem is that my fragments are pretty memory consuming so I would not like to have too much in memory. I used the same code with support library v4, but I had to update it to v13 to use NotificationCompat.Builder.addAction(int, CharSequence, PendingIntent).

你们中的任何人都知道可能是什么问题,以及什么是解决方案吗?

Does any of you know what could be the problem, and what could be the solution?

推荐答案

您的适配器是否处理大量项目? (很大>〜2 20 个项目)

Is your adapter handling very large amounts of items? (very large > ~220 items)

ViewPager.populate(int) 将从当前项目位置循环到

ViewPager.populate(int) will loop from the current item position to N, which is the number of items in the adapter - so if you've set it to something large (such as Integer.MAX_VALUE) this will takes some time, but will eventually finish.

如果是这种情况,请搜索与无尽传呼机"相关的问题,例如 Endless ViewPager android ,或将项目数量限制为对主线程合理的数量.

If this is your case, search for "endless pager" related questions, such as Endless ViewPager android, or limit the number of items to something that's reasonable for the main thread.

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

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