ViewPager + PagerAdapter显示前两个项目之后的空白页两个、项目、空白页、ViewPager

2023-09-05 09:39:58 作者:听弦断

这是怎么回事:前两个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.

调试信息:本网页的在的被实例化(见下文code),并成功添加到ViewPager 的ArrayList< ItemInfo> mItems (通过日志消息确认)。我重新编译了 android.support.v4 库调试= true设置( MMM框架/支持/ V4 / 在AOSP ),它显示了一切工作完美,除了:

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。它直接替换旧库视图,并为测试目的PagerAdapter instantiateItem(收集,位置),只需做到这一点:

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;
}

任何想法?这看起来对我来说有些神秘的浏览量的父母设定在某种程度上禁用onLayout浏览量

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().

这打破了我的视图层次的清新和prevented 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.

PRO提示: Android的SDK /工具/ hierarchyviewer 是不可思议的。谢谢Android团队。每个人都应该运行它至少一次,也可以是非常宝贵的调试和设计任何用户界面。

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.