So it appears that when using a ViewPager, the onPageSelected listener does not get called for the first page same issue as this.
I have some logic that populates some more expensive UI elements for the currently selected page and this works when page is changed, but it doesn't work for the first page.
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:
// 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);
Here view
ends up being null because PagerAdapter.instantiateItem
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 Activity.onAttachedToWindow
and Activity.onResume
but it appears both of these get fired before PagerAdapter.instantiateItem
.
ViewPager in Android is a class that allows the user to flip left and right through pages of data. This class provides the functionality to flip pages in app. It is a widget found in the support library. To use it you'll have to put the element inside your XML layout file that'll contain multiple child views.
Here's a way you can know the scroll direction while it's happening. All you have to do is set an OnPageChangeCallback() on the ViewPager . You save the current page of the ViewPager in OnPageSelected() and compare it to the position parameter of OnPageScrolled() .
Android ViewPager widget is found in the support library and it allows the user to swipe left or right to see an entirely new screen. Today we're implementing a ViewPager by using Views and PagerAdapter. Though we can implement the same using Fragments too, but we'll discuss that in a later tutorial.
You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .
I'm wondering why you don't just have the logic you mention in the Fragment itself rather than the hosting Activity. ViewPager buffers a couple of fragments either side of the current one so they're set up in the background and ready to go when the user swipes to them. Putting the logic in onPageSelected
would mean bypassing this functionality and not doing the heavy lifting until the user swipes to the page.
Assuming for some reason you can't do the above, why not use an Interface with a callback function. Trigger the callback in the fragment's onCreateView
function to let the Activity know it's fully instantiated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With