Note that there are some explanatory texts on larger screens.

plurals
  1. POViewPager onPageSelected for first page
    text
    copied!<p>So it appears that when using a ViewPager, the onPageSelected listener does not get called for the first page same issue as <a href="http://code.google.com/p/android/issues/detail?id=27526" rel="noreferrer">this</a>.</p> <p>I have some logic that populates some more expensive UI elements for the currently selected page and this works when page is changed, but <code>it doesn't work for the first page.</code> </p> <p>If I set the current item after the listener, the callback gets fired for the first page, but the view has not been initialized yet, so I can't manipulate it:</p> <pre><code>// Inside PagerAdapter.instantiateItem ViewHolder vh = new ViewHolder(); cursor.moveToPosition(position); vh.view = adapter.newView(context, cursor, null); // Set position as tag so we can retrieve it with findViewByTag vh.view.setTag(position); ((ViewPager) collection).addView(vh.view,0); return vh; // Inside MyActivity.onCreate pagerAdapter = new SingleMessagePagerAdapter(this, cursor); pager = (ViewPager)findViewById(R.id.message_pager); pager.setAdapter(pagerAdapter); pager.setOnPageSelectedListener(this); pager.setCurrentItem(selectedItem); // Inside MyActivity.onPageSelected // Retrieve tagged view View view = pager.findViewWithTag(position); </code></pre> <p>Here <code>view</code> ends up being null because <code>PagerAdapter.instantiateItem</code> has not yet been run. So I guess my question is, at which point in the activity lifecycle can I be certain that the ViewPager has initialized the view? I tried doing this inside <code>Activity.onAttachedToWindow</code> and <code>Activity.onResume</code> but it appears both of these get fired before <code>PagerAdapter.instantiateItem</code>.</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