Note that there are some explanatory texts on larger screens.

plurals
  1. POView SetVisibility not working with PagerAdapter
    text
    copied!<p><strong>EDIT:</strong> After more investigation, it turns out that this has nothing to do with the <code>ViewPager</code>. The issue is with setting a visibility attribute on a <code>ViewGroup</code> in XML, then attempting to change it at runtime. I'm leaving the original question as it is. See my answer <a href="https://stackoverflow.com/a/18108336/1139908">below</a> for more information.</p> <p>I have a layout with a <code>ViewPager</code> and a custom <code>PagerIndicator</code> class. On the 0th page of the <code>ViewPager</code>, I want the indicator to be <code>View.GONE</code>. On other pages, I want it to be <code>View.VISIBLE</code>. Here's my code, which is called during <code>onCreate</code>:</p> <pre><code>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) { } }); } </code></pre> <p>The <code>mPagerIndicator</code> is set with <code>android:visibility="invisible"</code> 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.</p> <p>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. (<strong>EDIT:</strong> 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.)</p> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload