Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>Basically, to accomplish this you have to keep track which position your Fragments have. Below is the implementation I used for my <code>FragmentPagerAdapter</code>, which uses an ArrayList as its data source.</p> <pre class="lang-java prettyprint-override"><code>public class MyFragmentPagerAdapter extends FragmentPagerAdapter { private HashMap&lt;Long, Fragment&gt; mItems = new HashMap&lt;Long, Fragment&gt;(); private ArrayList&lt;MyObject&gt; dataset; public MyFragmentPagerAdapter(ArrayList&lt;MyObject&gt; 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 &lt; 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&lt;Long, MainListFragment&gt; 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; } } </code></pre> <p>Now if you remove an item from the dataset (<code>this.dataset</code>) and call <code>notifyDataSetChanged()</code> on your instance of <code>MyFragmentPagerAdapter</code>, it will remove the item from the ViewPager (even if it's currently being viewed).</p> <p>Let's say <code>this.dataset</code> 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 <code>Collection.Sort</code> or some other way). I'll just show you the easy way.</p> <pre class="lang-java prettyprint-override"><code>ArrayList&lt;MyObject&gt; list = new ArrayList&lt;&gt;(); 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! </code></pre> <p><code>adapter.notifyDataSetChanged()</code> eventually calls <code>viewpager.dataSetChanged()</code>, which in turn calls <code>adapter.getItemPosition(..)</code> on each of its pages. The result of that method call determines if and where the page (Fragment in this case) will show up.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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