Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8555933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:14:17+00:00 2026-06-11T15:14:17+00:00

So it appears that when using a ViewPager, the onPageSelected listener does not get

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:14:19+00:00Added an answer on June 11, 2026 at 3:14 pm

    try to use fragments!

    public class SectionsPagerAdapter extends FragmentPagerAdapter {
    
        public SectionsPagerAdapter() {
            super(getSupportFragmentManager());
        }
    
        @Override
        public Fragment getItem(int i) {
            Fragment fr = null;
            if (i==0)
               fr = new sec0frag();
            else if (i==1)
               fr = new sec1frag();
            else if (i==2)
               fr = new sec2frag();
            return fr;
        }
    
        @Override
        public int getCount() {
            return 3;
        }
    }
    

    and create 3 fragments classes

    e.g. :

        public static class sec0frag extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
                         // here is initialize for first time to view pages.!
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It appears that SQLite does not enforce foreign keys by default. I'm using sqlitejdbc-v056.jar
Appears that WAS does not support the integration binding. I've tried setting it up
When positing large files as an InputStream using the Jersey client, it appears that
I have an activity that appears as a dialog using the following custom theme:
It appears that second windows are not closing. Here is what I am doing....
It appears that when using a having clause a count(*) is evaluated before the
I've written an app using a SurfaceView. It appears that one thing that is
From the OpenCV documentation, it appears that copying a matrix is done using a
Update: It appears that a recent XCode update may have changed this macro, please
It appears that using [] around a generator expression (test1) behaves substantially better than

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.