简体   繁体   English

视图SetVisibility不与PagerAdapter一起使用

[英]View SetVisibility not working with PagerAdapter

EDIT: After more investigation, it turns out that this has nothing to do with the ViewPager . 编辑:经过更多调查,结果发现这与ViewPager The issue is with setting a visibility attribute on a ViewGroup in XML, then attempting to change it at runtime. 问题在于在XML的ViewGroup上设置可见性属性,然后尝试在运行时进行更改。 I'm leaving the original question as it is. 我将保留原来的问题。 See my answer below for more information. 有关更多信息,请参见下面的答案。

I have a layout with a ViewPager and a custom PagerIndicator class. 我有一个带有ViewPager和自定义PagerIndicator类的布局。 On the 0th page of the ViewPager , I want the indicator to be View.GONE . ViewPager的第0页上,我希望指示器为View.GONE On other pages, I want it to be View.VISIBLE . 在其他页面上,我希望它是View.VISIBLE Here's my code, which is called during onCreate : 这是我的代码,在onCreate期间被调用:

void setupPager() {
    mPager.setAdapter(new TutorialPagerAdapter());
    mPager.setOnPageChangeListener(new OnPageChangeListener() {
        @Override
        public void onPageSelected(int currentPage) {
            Log.d(TAG, String.valueOf(currentPage));
            if (currentPage == 0) {
                mPagerIndicator.setVisibility(View.GONE);
            } else {
                mPagerIndicator.setVisibility(View.VISIBLE);
            }
            mPagerIndicator.setCurrentPage(currentPage);

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

The mPagerIndicator is set with android:visibility="invisible" in the XML layout. mPagerIndicator在XML布局中设置为android:visibility="invisible" When I scroll between pages, I can see that the callback is being called, and the page number is correct. 当我在页面之间滚动时,可以看到正在调用该回调,并且页面号正确。 However, the pager doesn't appear. 但是,没有出现寻呼机。

This is where it gets bizarre: I loaded up the Android Hierarchy Viewer that comes with the SDK. 这就是奇怪的地方:我加载了SDK随附的Android Hierarchy Viewer。 When it loaded the view hierarchy from the emulator, POOF, the indicator appeared. 当它从模拟器POOF加载视图层次结构时,指示符出现。 It also doesn't seem to be a problem on a physical device. 在物理设备上似乎也不是问题。 ( EDIT: After some more testing, it appears to be an issue with 2.3, as it doesn't happen on higher versioned devices, but does happen on a 2.3.6 phone.) 编辑:经过更多测试后,2.3似乎是一个问题,因为版本较高的设备上不会发生,但2.3.6手机上确实会发生。)

Any idea why this is happening? 知道为什么会这样吗? Is it reasonable to assume that this is just a quirk of the emulator, or should I be worried that it won't work on some devices? 假设这只是仿真器的一个怪癖,是否合理?还是我应该担心它在某些设备上无法使用? Any hacks to get it to show up? 有什么技巧可以显示吗? What does the hierarchy viewer do that might be forcing it to refresh itself? 层次结构查看器做什么,可能会迫使它刷新自身?

What do you mean by "it doesn't seem to be a problem on a physical device"? 您“在物理设备上似乎不是问题”是什么意思? Do you mean you see the indicator OK, or that the indicator is visible in the HierarchyViewer? 您是说您看到指示器OK,还是在HierarchyViewer中可见指示器?

Everything looks OK to me in your code. 在您的代码中,一切对我来说都不错。 Sounds like your indicator is simply covered by another view - maybe the ViewPager - but it exists just fine, therefore you can see it in the HierarchyViewer. 听起来您的指示器只是被另一个视图(也许是ViewPager)覆盖了,但是它存在的很好,因此您可以在HierarchyViewer中看到它。

Try removing the code above to toggle visibility, then set your PagerIndicator to View.VISIBLE in the xml. 尝试删除上面的代码以切换可见性,然后在xml中将View.VISIBLE设置为View.VISIBLE If you still can't see it, then there's your problem. 如果仍然看不到,那就是您的问题。

It appears that there is an issue in Android 2.3.3+ involving visibility of ViewGroups . 似乎Android 2.3.3+中存在一个涉及ViewGroups可见性的问题。 Setting the ViewGroup with android:visibility=X in the XML seems to set the visibility of all of the subviews. 在XML中将android:visibility=X设置为ViewGroup似乎可以设置所有子视图的可见性。 However, when changing the visibility at runtime, it does not apply it to the subviews. 但是,在运行时更改可见性时,不会将其应用于子视图。 Because of this, starting with a visibility of gone or invisibile will cause setting the visibility later to fail. 因此,从可见性goneinvisibile开始,将导致稍后设置可见性失败。

The solution is to override setVisibility in my custom view, to make it set the visibility on all of its subviews as well: 解决方案是在我的自定义视图中覆盖setVisibility ,以使其也设置其所有子视图的可见性:

@Override
public void setVisibility(int visibility) {
    super.setVisibility(visibility);
    for (ImageView image : mImages) {
        image.setVisibility(visibility);
    }
}

Thanks to http://www.kittehface.com/2011/03/view-visibility-bug-on-android-233.html and android setVisibility does not display if initially set to invisble for getting me pointed in the right direction. 多亏了http://www.kittehface.com/2011/03/view-visibility-bug-on-android-233.htmlandroid setVisibility,如果最初设置为invisble不会使我指向正确的方向, 则不会显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM