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

  • SEARCH
  • Home
  • 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 8297301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:22:00+00:00 2026-06-08T15:22:00+00:00

I’m using a ViewPager from ViewPageIndicator and I need to be able to dynamically

  • 0

I’m using a ViewPager from ViewPageIndicator and I need to be able to dynamically insert one fragment in the middle of others.

I’ve tried to manage the model with FragmentPagerAdapter and FragmentStatePagerAdapter (both from v4 support code) and the first seems to don’t manage in any way insertion of pages in the middle.
And the second only works if I make a naive implementation of getItemPosition returning always POSITION_NONE but this cause completly recreation of pages every time I swipe.

The problem I’ve observed with FragmentStatePagerAdapter (FSP) is this:

  • I start with two pages [A][B]
  • Then I insert [C] in the middle [A][C][B]. After the insert I call notifyDataSetchange()
  • Then FSP calls getItemPosition for [A] and gets 0
  • Then FSP calls geTItemPosition for [B] and gets 2. And it says… Ohhh I’ve to destroy [B] and makes mFragments.set(2, null) and then as it only have two elements in mFragments array it throws an IndexOutOfBoundsException

After looking a little in the code it seems that provided fragmentStatePagerAdapter doesn’t support insertion in the middle. Is that correct or I missing something?

Update:
The insert in the adapter is made in a logical way, when a certain codition is true the pages are incremented by one. The fragment creation is done using constructor in getItem() in that way:

void setCondition(boolean condition) {
   this.condition=condition;
   notifyDataSetChanged();
}
public int getCount(){
    return condition?3:2;
}
public Fragment getItem(int position) {
    if(position==0) 
        return new A();
    else if(position==1)
        return condition?new C():new B();
    else if(position==2 && condition)
        return new B();
    else throw new RuntimeException("Not expected");
}
public int getItemPosition(Object object) {
    if(object instanceof A) return 0;
    else if(object instanceof B) return condition?2:1;
    else if(object instanceof C) return 1;
} 

Solution:

As said in accepted answer the key is to implement getItemId().

Make sure you use at least the R9(June 2012) version of android-support library. Because this method was added in it. Previous to this version that method doesn’t exist and adapter doesn’t manage correctly insertions.
Also make sure you use FragmentPageAdapter because FragmentStatePagerAdapter still doesn’t works because it doesn’t use ids.

  • 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-08T15:22:06+00:00Added an answer on June 8, 2026 at 3:22 pm

    You forgot about one method.
    Override getItemId(int position) from FragmentPagerAdapter which simply returns position to return something what will identify fragment instance.

    public long getItemId(int position) {
        switch (position) {
        case 0:
            return 0xA;
        case 1:
            return condition ? 0xC : 0xB;
        case 2:
            if (condition) {
                return 0xB;
            }
        default:
            throw new IllegalStateException("Position out of bounds");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... 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.