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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:30:46+00:00 2026-06-15T10:30:46+00:00

(I figured out a solution – please see my post in the Answer section

  • 0

(I figured out a solution – please see my post in the Answer section below.)

In my app, the user will start with a single view of his data. I’d like to add a ViewPager and allow the user to add more views as desired. How do I do this? (I dont’ want to use the FragmentPagerAdapter.)

I’ve read countless posts and overviews but am still missing something. Here’s what I think I understand:

MainActivity creates a ViewPager and PagerAdapter:

ViewPager pager = null;
MainPagerAdapter adapter = null;
public void onCreate (Bundle savedInstanceState)
{
  super.onCreate (savedInstanceState);
  pager = new ViewPager (this);
  setContentView (pager);

  adapter = new MainPagerAdapter();
  pager.setAdapter (adapter); 

  View v0 = code_to_create_initial_view();
  adapter.add (v0, 0);      
}

Use a PagerAdapter to provide the sets of view. For this it seems I need methods to add and remove views, something like this; obviously more is needed to tell the ViewPager stuff has changed and how to show the change:

class MainPagerAdapter extends PagerAdapter
{
  // This holds all the currently displayable views, in order from left to right.
  private ArrayList<View> views = new ArrayList<View>();

  public void addView (View v, int position)
  {
    views.add (position, v);
  }

  public void removeView (int position)
  {
    views.remove (position);
  }
}

In addition, I need to implement the following vitual methods. I’m lost here – what calls them and what are they supposed to do (ok, getCount is obvious)?

  public object instantiateItem (ViewGroup pager, int position);
  public void destroyItem (ViewGroup, int, Object);
  public int getCount ();
  public boolean isViewFromObject (View, Object);
  • What are the ViewGroup params for – isn’t the containing group the ViewPager itself?
  • What does isViewFromObject do – how does an object get associated with a view in the first place?
  • Should I be calling startUpdate and finishUdate when I add or remove views?

Thanks.

  • 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-15T10:30:46+00:00Added an answer on June 15, 2026 at 10:30 am

    After figuring out which ViewPager methods are called by ViewPager and which are for other purposes, I came up with a solution. I present it here since I see a lot of people have struggled with this and I didn’t see any other relevant answers.

    First, here’s my adapter; hopefully comments within the code are sufficient:

    public class MainPagerAdapter extends PagerAdapter
    {
      // This holds all the currently displayable views, in order from left to right.
      private ArrayList<View> views = new ArrayList<View>();
    
      //-----------------------------------------------------------------------------
      // Used by ViewPager.  "Object" represents the page; tell the ViewPager where the
      // page should be displayed, from left-to-right.  If the page no longer exists,
      // return POSITION_NONE.
      @Override
      public int getItemPosition (Object object)
      {
        int index = views.indexOf (object);
        if (index == -1)
          return POSITION_NONE;
        else
          return index;
      }
    
      //-----------------------------------------------------------------------------
      // Used by ViewPager.  Called when ViewPager needs a page to display; it is our job
      // to add the page to the container, which is normally the ViewPager itself.  Since
      // all our pages are persistent, we simply retrieve it from our "views" ArrayList.
      @Override
      public Object instantiateItem (ViewGroup container, int position)
      {
        View v = views.get (position);
        container.addView (v);
        return v;
      }
    
      //-----------------------------------------------------------------------------
      // Used by ViewPager.  Called when ViewPager no longer needs a page to display; it
      // is our job to remove the page from the container, which is normally the
      // ViewPager itself.  Since all our pages are persistent, we do nothing to the
      // contents of our "views" ArrayList.
      @Override
      public void destroyItem (ViewGroup container, int position, Object object)
      {
        container.removeView (views.get (position));
      }
    
      //-----------------------------------------------------------------------------
      // Used by ViewPager; can be used by app as well.
      // Returns the total number of pages that the ViewPage can display.  This must
      // never be 0.
      @Override
      public int getCount ()
      {
        return views.size();
      }
    
      //-----------------------------------------------------------------------------
      // Used by ViewPager.
      @Override
      public boolean isViewFromObject (View view, Object object)
      {
        return view == object;
      }
    
      //-----------------------------------------------------------------------------
      // Add "view" to right end of "views".
      // Returns the position of the new view.
      // The app should call this to add pages; not used by ViewPager.
      public int addView (View v)
      {
        return addView (v, views.size());
      }
    
      //-----------------------------------------------------------------------------
      // Add "view" at "position" to "views".
      // Returns position of new view.
      // The app should call this to add pages; not used by ViewPager.
      public int addView (View v, int position)
      {
        views.add (position, v);
        return position;
      }
    
      //-----------------------------------------------------------------------------
      // Removes "view" from "views".
      // Retuns position of removed view.
      // The app should call this to remove pages; not used by ViewPager.
      public int removeView (ViewPager pager, View v)
      {
        return removeView (pager, views.indexOf (v));
      }
    
      //-----------------------------------------------------------------------------
      // Removes the "view" at "position" from "views".
      // Retuns position of removed view.
      // The app should call this to remove pages; not used by ViewPager.
      public int removeView (ViewPager pager, int position)
      {
        // ViewPager doesn't have a delete method; the closest is to set the adapter
        // again.  When doing so, it deletes all its views.  Then we can delete the view
        // from from the adapter and finally set the adapter to the pager again.  Note
        // that we set the adapter to null before removing the view from "views" - that's
        // because while ViewPager deletes all its views, it will call destroyItem which
        // will in turn cause a null pointer ref.
        pager.setAdapter (null);
        views.remove (position);
        pager.setAdapter (this);
    
        return position;
      }
    
      //-----------------------------------------------------------------------------
      // Returns the "view" at "position".
      // The app should call this to retrieve a view; not used by ViewPager.
      public View getView (int position)
      {
        return views.get (position);
      }
    
      // Other relevant methods:
    
      // finishUpdate - called by the ViewPager - we don't care about what pages the
      // pager is displaying so we don't use this method.
    }
    

    And here’s some snips of code showing how to use the adapter.

    class MainActivity extends Activity
    {
      private ViewPager pager = null;
      private MainPagerAdapter pagerAdapter = null;
    
      //-----------------------------------------------------------------------------
      @Override
      public void onCreate (Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView (R.layout.main_activity);
    
        ... do other initialization, such as create an ActionBar ...
    
        pagerAdapter = new MainPagerAdapter();
        pager = (ViewPager) findViewById (R.id.view_pager);
        pager.setAdapter (pagerAdapter);
    
        // Create an initial view to display; must be a subclass of FrameLayout.
        LayoutInflater inflater = context.getLayoutInflater();
        FrameLayout v0 = (FrameLayout) inflater.inflate (R.layout.one_of_my_page_layouts, null);
        pagerAdapter.addView (v0, 0);
        pagerAdapter.notifyDataSetChanged();
      }
    
      //-----------------------------------------------------------------------------
      // Here's what the app should do to add a view to the ViewPager.
      public void addView (View newPage)
      {
        int pageIndex = pagerAdapter.addView (newPage);
        // You might want to make "newPage" the currently displayed page:
        pager.setCurrentItem (pageIndex, true);
      }
    
      //-----------------------------------------------------------------------------
      // Here's what the app should do to remove a view from the ViewPager.
      public void removeView (View defunctPage)
      {
        int pageIndex = pagerAdapter.removeView (pager, defunctPage);
        // You might want to choose what page to display, if the current page was "defunctPage".
        if (pageIndex == pagerAdapter.getCount())
          pageIndex--;
        pager.setCurrentItem (pageIndex);
      }
    
      //-----------------------------------------------------------------------------
      // Here's what the app should do to get the currently displayed page.
      public View getCurrentPage ()
      {
        return pagerAdapter.getView (pager.getCurrentItem());
      }
    
      //-----------------------------------------------------------------------------
      // Here's what the app should do to set the currently displayed page.  "pageToShow" must
      // currently be in the adapter, or this will crash.
      public void setCurrentPage (View pageToShow)
      {
        pager.setCurrentItem (pagerAdapter.getItemPosition (pageToShow), true);
      }
    }
    

    Finally, you can use the following for your activity_main.xml layout:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
    </android.support.v4.view.ViewPager>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I figured out the answer to this question, but I couldn't find the solution
So I figured out that by adding the ResetWebServer=FALSE attribute to the solution manifest
EDIT: I figured out the answer to the original YUI3 question I posted here,
Edit: I think I figured out the solution, but I'm still interested to know
This is probably easily figured out, but I can't find a solution anywhere, for
I know, this seems a bad solution, but I can't think or figured out
EDIT: I figured out the solution. I was not adding -combine to my compile
So I figured out that the replace function, that finds all single-letter words (prepositions
EDIT - I've figured out the solution to my problem and posted a Q&A
while I wrote this question I figured out a solution but I'm going to

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.