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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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.Now if you remove an item from the dataset (
this.dataset) and callnotifyDataSetChanged()on your instance ofMyFragmentPagerAdapter, it will remove the item from the ViewPager (even if it’s currently being viewed).Let’s say
this.datasetcontains 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 viaCollection.Sortor some other way). I’ll just show you the easy way.adapter.notifyDataSetChanged()eventually callsviewpager.dataSetChanged(), which in turn callsadapter.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.