ViewPager + PagerAdapter 在前两项后显示空白页

ViewPager + PagerAdapter shows blank pages after first two items(ViewPager + PagerAdapter 在前两项后显示空白页)

本文介绍了ViewPager + PagerAdapter 在前两项后显示空白页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发生了什么:前两个 ViewPager 页面加载良好(ViewPager 自动从当前页面加载 +-1).当您滚动过去时,不会显示下一页.以下问题的 GIF 演示.

What's happening: The first two ViewPager pages loaded nicely (ViewPager automatically loads +-1 from current page). When you scroll past these the next pages aren't showing up. GIF demo of the problem below.

调试信息:页面正在被实例化(参见下面的代码)并成功添加到 ViewPager ArrayListmItems(通过日志消息确认).我重新编译了带有 debug=true 集的 android.support.v4 库(aosp 中的 mmm frameworks/support/v4/),它显示一切正常,除了:

Debug info: The pages are being instantiated (see code below) and are successfully added to the ViewPager ArrayList<ItemInfo>mItems (confirmed via Log messages). I recompiled the android.support.v4 library with debug=true set (mmm frameworks/support/v4/ in aosp) and it shows everything working perfectly EXCEPT:

在加载前两个视图后,ViewPager 中的 onLayout 和 onMeasure 方法将停止调用.这意味着其他页面是黑色的,因为它们从未按照应有的方式进行测量和排列.我尝试在不同的点添加不同的 requestLayout() 调用以强制布局树刷新无济于事.

The onLayout and onMeasure methods in ViewPager stop being called after the first two views are loaded. This means the other pages are black because they were never measured&arranged as they should have been. I tried adding different requestLayout() calls at different points to force a layout tree refresh to no avail.

背景:我有一个通过 inflater.inflate 实例化并通过 view.findViewById(R.layouy.myid) 找到的 ViewPager.它直接替换了旧的 Gallery 视图,出于测试目的,PagerAdapter instantiateItem(collection, position) 只是这样做:

Background: I have a ViewPager instantiated via inflater.inflate and found via view.findViewById(R.layouy.myid). It's directly replacing an old Gallery view, and for testing purposes the PagerAdapter instantiateItem(collection, position) simply does this:

@Override
public Object instantiateItem(View collection, final int position) {
    final TextView tv = new TextView(collection.getContext());
    tv.setText("Looking at page " + position);
    tv.setTextColor(Color.WHITE);
    tv.setBackgroundColor(Color.rgb( (int)(Math.random()*100), (int)(Math.random()*100), (int)(Math.random()*100)));
    tv.setTextSize(22);
    ((ViewPager) collection).addView(tv);
    return tv;
}

有什么想法吗?在我看来,PageView 的父母中的一些神秘设置以某种方式禁用了 onLayout PageView

Any ideas? It looks to me like some mystery setting in PageView's parents are somehow disabling onLayout PageView

推荐答案

找到了我的问题.在我的视图的父级的父级中,有人继承了 LinearLayout 并覆盖了 requestLayout() 而没有调用 super.requestLayout().

Found my problem. In the parent of the parent of my view someone subclassed LinearLayout and overrode requestLayout() without calling super.requestLayout().

这破坏了我的视图层次结构的刷新,并阻止了 onMeasure 和 onLayout 在我的 ViewPager 上被调用.没有测量和布置页面,它们在 ViewPager 中显示为空白.

This broke the refreshing of my view hierarchy and prevented onMeasure and onLayout from being called on my ViewPager. Without measuring and laying out the pages they showed up as blank in ViewPager.

希望有一天这可以帮助其他人.我确实花了很长时间才弄清楚.

Hope this helps someone else out someday. It sure took me forever to figure out.

专业提示:android-sdk/tools/hierarchyviewer 令人难以置信.谢谢安卓团队.每个人都应该至少运行一次它,它对于调试和设计任何 UI 都是非常宝贵的.

PRO TIP: android-sdk/tools/hierarchyviewer is incredible. Thank you Android team. Everyone should run it at least once, it can be invaluable in debugging and designing anything UI.

这篇关于ViewPager + PagerAdapter 在前两项后显示空白页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:ViewPager + PagerAdapter 在前两项后显示空白页

基础教程推荐