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 9165445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:54:43+00:00 2026-06-17T14:54:43+00:00

I am new to using a ViewPager , but the initial screen on my

  • 0

I am new to using a ViewPager, but the initial screen on my app is a ListView that the user can add/remove new items to, then clicking on one of the items brings them to a “details” fragment based on an id that is passed. I’d like for the user to also be able to swipe from the listing through to each of the details.

I have the ViewPager working, except the id’s are always off by one. This might be my lack of understanding of ViewPagers, but if I put a breakpoint in the onCreateView of the details fragment, the breakpoint is hit when the app loads and the id that is passed is the first id. So, say the ids are 1,2,3,4, when the app loads, the id on app start-up in onCreateView is 1. When I perform the initial swipe from the listing to the first details fragment, the id is 2 (when I would expect it to be 1).

This is what I have so far:

Main.class (this initial activity on app start-up)

public class Main extends SherlockFragmentActivity
{
    private static int NUMBER_OF_PAGES;  
    private ViewPager mViewPager;  
    private MyFragmentPagerAdapter mMyFragmentPagerAdapter; 
    private static List<Fragment> fragments;

    @Override
    public void onCreate(final Bundle icicle)
    {    
        super.onCreate(icicle);

        setContentView(R.layout.main);

        mViewPager = (ViewPager)findViewById(R.id.viewpager);  
        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());  
        mViewPager.setAdapter(mMyFragmentPagerAdapter);

        final List<Integer> ids = GetIds(); //loads ids to popular the viewpager

        NUMBER_OF_PAGES = ids.size();

        fragments = new ArrayList<Fragment>();

        fragments.add(new ListingFragment()); //initial screen

        for(Integer id : ids)
            fragments.add(DetailsFragment.newInstance(id));
    }

    private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter  {            

        public MyFragmentPagerAdapter(FragmentManager fm) {  
             super(fm);  
        }

        @Override  
        public Fragment getItem(int index) {

            return fragments.get(index);
        }

        @Override  
        public int getCount() {  

             return NUMBER_OF_PAGES;  
        }
   }
}

DetailsFragment.class

public class DetailsFragment extends SherlockFragmentActivity
{
    private int detailId;
    private List<Integer> items;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);         
    }

    @Override
    public void onActivityCreated(final Bundle icicle)
    {    
        super.onActivityCreated(icicle);
    }

    public static DetailsFragment newInstance(int id) {

        DetailsFragment lf = new DetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        lf.setArguments(bundle);
        return lf;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.details, container, false);

        detailId = getArguments().getInt("id"); //this id is always off

        items = GetItems();

        return view;
    }
}
  • 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-17T14:54:44+00:00Added an answer on June 17, 2026 at 2:54 pm

    I have the ViewPager working, except the id’s are always off by one.
    This might be my lack of understanding of ViewPagers, but if I put a
    breakpoint in the onCreateView of the details fragment, the breakpoint
    is hit when the app loads and the id that is passed is the first id.
    So, say the ids are 1,2,3,4, when the app loads, the id on app
    start-up in onCreateView is 1. When I perform the initial swipe from
    the listing to the first details fragment, the id is 2 (when I would
    expect it to be 1).

    Maybe I don’t understand the problem you’re facing but the code you posted(again, if that is the full code you use(currently it will not compile as your DetailsFragments extends SherlockFragmentActivity (?!))) can’t do what you say. The way you setup the fragments will make the them have the proper ids.

    What you’re seeing it’s most likely due to the way the ViewPager works, which can be misleading. When you first start the app you’ll see the ListingFragment. Now, the ViewPager, in order to provide a smooth swipe for the user will also load(by default) one additional fragment on each side of the current visible fragment(so when the app starts the ViewPager will load page 0(ListingFragment) and the next(to the right as we don’t have anywhere to go left)) page, 1(the DetailsFragment with the id 1). When you swipe from the ListingFragment to the first DetailsFragment(which has the id 1) the ViewPager will automatically create the second DetailsFragment(which has the id 2) in advance(again, in order to be able to swipe right away to it). Now, if you have a Log statement in the onCreateView in the DetailsFragment(to see the id) it’s normal that you see the second id as you swipe from the ListingFragment to the first DetailsFragment because that Log will not appear from the first DetailsFragment(with id 1, which is already created) being created, it will appear because the second DetailsFragment(with id 2) is being created in advance.

    Also, revise your code, as you introduced a subtle bug. You set the size of the adapter to
    NUMBER_OF_PAGES which is initialized with ids.size() but then you add the ListingFragment which will make the DetailsFragment with the last id(from ids) not appear at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a ViewPager in my app, with the initial fragment being a ListView
I'm using the new ViewPager -view from the Android compatibility library, and I can't
In my Android application, The user can browse some HTML pages using ViewPager, and
I'm new using Yii framework. I show a list a checkbox. But it's not
I am new to using Jade -- and it's awesome so far. But one
I am using viewPager and would like to add fragments to it: public class
I am developing application using viewpager with fragment.In my application i have a items
I am using a ViewPager with a custom PagerFragmentAdapter that uses an internal list
I'm using a ViewPager that contains several ListViews, with code similar to that in
I try to write an app, that is using slide effect for activities and

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.