ViewPager不重绘的内容,仍然/变成空白 [英] ViewPager does not redraw content, remains/turns blank

查看:286
本文介绍了ViewPager不重绘的内容,仍然/变成空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在遭受一个很奇怪的问题,ViewPager在这里。我们嵌入列出每个ViewPager网页上,并更新列表数据时触发notifyDataSetChanged两个列表适配器和看法寻呼机适配器上。

We're suffering from a very strange issue with ViewPager here. We embed lists on each ViewPager page, and trigger notifyDataSetChanged both on the list adapter and the view pager adapter when updating list data.

我们所观察到的是,有时,网页不更新其视图树,即仍然是黑屏,有时甚至分页到它的时候消失。当寻呼来回几次,内容会突然出现。看来,如果Android正在这里缺少一个视图更新。我还注意到,有层次观众在调试的时候,选择一个视图总是会让它重新出现,显然是因为层次观众强制所选视图重绘自身。

What we observe is that sometimes, the page does not update its view tree, i.e. remains blank, or sometimes even disappears when paging to it. When paging back and forth a few times, the content will suddenly reappear. It seems as if Android is missing a view update here. I also noticed that when debugging with hierarchy viewer, selecting a view will always make it reappear, apparently because hierarchy viewer forces the selected view to redraw itself.

我不能做这项工作虽然编程;无效的列表视图,或整个视图寻呼机甚至,都没有效果。

I could not make this work programmatically though; invalidating the list view, or the entire view pager even, had no effect.

这是与兼容性v4_r7库。我也尝试使用最新版本,因为它声称要修复与查看传呼机许多问题,但它使事情更糟(例如,手势被打破,这样它不会让我翻阅所有页面了的时候。)

This is with the compatibility-v4_r7 library. I also tried to use the latest revision, since it claims to fix many issues related to view pager, but it made matters even worse (for instance, gestures were broken so that it wouldn't let me page through all pages anymore sometimes.)

是任何人都遇到了这些问题,也或者你有什么可能会造成这一点的想法?

Is anyone else running into these issues, too, or do you have an idea of what could be causing this?

推荐答案

我们终于设法找到解决的办法。显然,我们的实施遭遇的两个问题:

We finally managed to find a solution. Apparently our implementation suffered of two issues:

  1. 在我们的适配器没有删除 destroyItem视图()
  2. 我们在缓存意见,使我们不得不抬高我们的布局只有一次,而且,因为我们并没有去除 destroyItem视图(),我们没有将其添加在 instantiateItem(),但只返回对应于当前位置缓存的视图。
  1. our adapter did not remove the view in destroyItem().
  2. we were caching views so that we'd have to inflate our layout just once, and, since we were not removing the view in destroyItem(), we were not adding it in instantiateItem() but just returning the cached view corresponding to the current position.

我没有看太深深的来源$ C ​​$ c中的 ViewPager - 这是不完全明确,你必须做到这一点 - 但该文档说:

I haven't looked too deeply in the source code of the ViewPager - and it's not exactly explicit that you have to do that - but the docs says :

destroyItem()
删除页面指定位置。适配器负责清除从它的容器的视图,但它只有必须保证,这是通过从它finishUpdate(ViewGroup中)。

destroyItem()
Remove a page for the given position. The adapter is responsible for removing the view from its container, although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup).

有一个非常简单的PagerAdapter可以选择使用网页浏览自己作为重点对象,创建后从instantiateItem(ViewGroup中,INT)返回它们并将它们添加到父ViewGroup中。的匹配destroyItem(ViewGroup中,整型,对象)的实施将消除来自父的ViewGroup和isViewFromObject(查看,对象)可以被实现为返回查看查看==对象。

A very simple PagerAdapter may choose to use the page Views themselves as key objects, returning them from instantiateItem(ViewGroup, int) after creation and adding them to the parent ViewGroup. A matching destroyItem(ViewGroup, int, Object) implementation would remove the View from the parent ViewGroup and isViewFromObject(View, Object) could be implemented as return view == object;.

所以,我的结论是, ViewPager 依赖其底层适配器上明确添加/删除 instantiateItem其子() / destroyItem()。也就是说,如果你的适配器 PagerAdapter 的子类,子类必须实现这个逻辑。

So my conclusion is that ViewPager relies on its underlying adapter to explicitly add/remove its children in instantiateItem()/destroyItem(). That is, if your adapter is a subclass of PagerAdapter, your subclass must implement this logic.

附注:需要注意的<一个href="http://stackoverflow.com/questions/7290841/java-lang-illegalargumentexception-the-observer-is-null">this如果你里面使用列表 ViewPager

Side note: be aware of this if you use lists inside ViewPager.

这篇关于ViewPager不重绘的内容,仍然/变成空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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