简体   繁体   中英

View SetVisibility not working with PagerAdapter

EDIT: After more investigation, it turns out that this has nothing to do with the ViewPager . The issue is with setting a visibility attribute on a ViewGroup in XML, then attempting to change it at runtime. 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. On the 0th page of the ViewPager , I want the indicator to be View.GONE . On other pages, I want it to be View.VISIBLE . Here's my code, which is called during 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. 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. When it loaded the view hierarchy from the emulator, POOF, the indicator appeared. 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.)

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?

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.

Try removing the code above to toggle visibility, then set your PagerIndicator to View.VISIBLE in the xml. 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 . Setting the ViewGroup with android:visibility=X in the XML seems to set the visibility of all of the subviews. 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.

The solution is to override setVisibility in my custom view, to make it set the visibility on all of its subviews as well:

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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