(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.
I was looking for simple solution to remove views from viewpager (no fragments) dynamically. So, if you have some info, that your pages belongs to, you can set it to View as tag. Just like that (adapter code):
You just need remove your info from mMessages, and then call
notifyDataSetChanged()
for your adapter. Bad news there is no animation in this case.I did a similar program. hope this would help you.In its first activity four grid data can be selected. On the next activity, there is a view pager which contains two mandatory pages.And 4 more pages will be there, which will be visible corresponding to the grid data selected.
Following is the mainactivty MainActivity
Here TabFragment1,TabFragment2 etc are fragment to be displayed on the viewpager.And I am not showing the layouts since they are out of scope of this project.
MainActivity will intent to Main2Activity Main2Activity
ViewPagerAdapter Viewpageradapter.class
Layout for main2activity acticity_main2.xml
Hope this would help someone... Click up button please if this helped you
I find a good solution for this issue, this solution can make it work and no need to recreate Fragments.
My key point is:
Source Code
Code is at my Github Gist.
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:
And here's some snips of code showing how to use the adapter.
Finally, you can use the following for your
activity_main.xml
layout:Here's an alternative solution to this question. My adapter:
Code to remove and add dynamically
After the advice of Peri Hartman, it started to work after I set null do ViewPager adapter and put the adapter again after the views removed. Before this the page 0 doesnt showed its list contents.
Thanks.
There are quite a few discussions around this topic
Although we see it often, using
POSITION_NONE
does not seem to be the way to go as it is very inefficient memory-wise.Here in this question, we should consider using Alvaro's approach:
Here is a SO answer with code based on this idea