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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:28:29+00:00 2026-06-18T15:28:29+00:00

Using the Viewpager in Android, is it possible to programmatically move any page to

  • 0

Using the Viewpager in Android, is it possible to programmatically move any page to the end? For example, I have five pages and want to move page #2 to the last position (5th position) so page 3 becomes 2, 4 becomes 3, and 5 becomes 4?

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-18T15:28:30+00:00Added an answer on June 18, 2026 at 3:28 pm

    Yes. I’ll try and answer your case by showing how I did it, and I’ll show an example for your case below my code sample.

    Basically, to accomplish this you have to keep track which position your Fragments have. Below is the implementation I used for my FragmentPagerAdapter, which uses an ArrayList as its data source.

    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
    
        private HashMap<Long, Fragment> mItems
                        = new HashMap<Long, Fragment>();
    
        private ArrayList<MyObject> dataset;
    
        public MyFragmentPagerAdapter(ArrayList<MyObject> objects) {
            this.dataset = objects;
        }
    
        @Override
        public int getCount() {
            return dataset.size();
        }
    
        @Override
        public Fragment getItem(int position) {
            long id = getItemId(position);
    
            if(mItems.get(id) != null) {
                // caching to prevent multiple instances of the same fragment
                // for the same position/id
                return mItems.get(id);
            }
    
            Fragment f = Fragment.newInstance();
    
            mItems.put(id, f);
    
            return f;
        }
    
        @Override
        public long getItemId(int position) {
            // return a unique id
            return dataset.get(position).getUniqueId();
        }
    
        @Override
        public int getItemPosition(Object object) {
            /*
             * Purpose of this method is to check whether an item in the adapter
             * still exists in the dataset and where it should show.
             * For each entry in dataset, request its Fragment.
             * 
             * If the Fragment is found, return its (new) position. There's
             * no need to return POSITION_UNCHANGED; ViewPager handles it.
             * 
             * If the Fragment passed to this method is not found, remove all
             * references and let the ViewPager remove it from display by
             * by returning POSITION_NONE;
             */
            Fragment f = (Fragment) object;
    
            for(int i = 0; i < getCount(); i++) {
    
                Fragment item = (Fragment) getItem(i);
                if(item.equals(f)) {
                    // item still exists in dataset; return position
                    return i;
                }
            }
    
            // if we arrive here, the data-item for which the Fragment was created
            // does not exist anymore.
    
            // Also, cleanup: remove reference to Fragment from mItems
            for(Map.Entry<Long, MainListFragment> entry : mItems.entrySet()) {
                if(entry.getValue().equals(f)) {
                    mItems.remove(entry.getKey());
                    break;
                }
            }
    
            // Let ViewPager remove the Fragment by returning POSITION_NONE.
            return POSITION_NONE;
        }
    }
    

    Now if you remove an item from the dataset (this.dataset) and call notifyDataSetChanged() on your instance of MyFragmentPagerAdapter, it will remove the item from the ViewPager (even if it’s currently being viewed).

    Let’s say this.dataset contains 5 items, and you want to move #2 to the end of the ViewPager. To accomplish this, you’ll have to position item#2 to the end of your datasource (either via Collection.Sort or some other way). I’ll just show you the easy way.

    ArrayList<MyObject> list = new ArrayList<>();
    MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(list);
    viewpager.setAdapter(adapter);
    
    ...
    
    MyObject item = list.get(2);
    list.remove(item);
    list.add(item); // now it's positioned at the end of the list
    adapter.notifyDataSetChanged(); // voilá, that should do the trick!
    

    adapter.notifyDataSetChanged() eventually calls viewpager.dataSetChanged(), which in turn calls adapter.getItemPosition(..) on each of its pages. The result of that method call determines if and where the page (Fragment in this case) will show up.

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

Sidebar

Related Questions

I have a ViewPager using the Android support framework, which loads three fragments. The
In my Android application, The user can browse some HTML pages using ViewPager, and
I want to change the animation style of Android's ViewPager class with using Android
I have a working viewpager example using code I got from a tutorial. The
In my android application Iam using viewpager.I have two fragments.Fragment1 and Fragment2.Iam calling two
I am using a android.support.v4.view.ViewPager and wish to page through its views quickly. Currently,
I have used the android v4 viewPager widget. I want to go to a
I am using Android's support.v4 package to develop a ViewPager containing multiple Fragment s.
I have a FragmentActivity using a ViewPager to serve several fragments. Each is a
I have the following problem. I have a tabbed application implemented using a ViewPager.

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.