Note that there are some explanatory texts on larger screens.

plurals
  1. POdynamically add and remove view to viewpager
    primarykey
    data
    text
    <p><strong>(I figured out a solution - please see my post in the Answer section below.)</strong></p> <p>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.)</p> <p>I've read countless posts and overviews but am still missing something. Here's what I think I understand:</p> <p>MainActivity creates a ViewPager and PagerAdapter:</p> <pre><code>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); } </code></pre> <p>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:</p> <pre><code>class MainPagerAdapter extends PagerAdapter { // This holds all the currently displayable views, in order from left to right. private ArrayList&lt;View&gt; views = new ArrayList&lt;View&gt;(); public void addView (View v, int position) { views.add (position, v); } public void removeView (int position) { views.remove (position); } } </code></pre> <p>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)? </p> <pre><code> public object instantiateItem (ViewGroup pager, int position); public void destroyItem (ViewGroup, int, Object); public int getCount (); public boolean isViewFromObject (View, Object); </code></pre> <ul> <li>What are the ViewGroup params for - isn't the containing group the ViewPager itself? </li> <li>What does isViewFromObject do - how does an object get associated with a view in the first place?</li> <li>Should I be calling startUpdate and finishUdate when I add or remove views?</li> </ul> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload