Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tabs + ViewPager + FragmentStatePagerAdapter - How to remove fragment?

I'm using the "Tabs + Swipe" project and I'm having a difficult time removing a fragment.

Steps I'm doing:

  1. Remove tab from database
  2. Remove tab from the FragmentStatePagerAdapter data source
  3. Remove tab from the actionBar.
  4. Remove fragment using Support FragmentManager.

The problem: After I perform the remove, for some reason, I can still scroll to the right and see an empty fragment. I can't select it, it just bounces back. It seems like the fragment is not being removed but rather changes its position to the tag from the left.

My Adapter:

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new TabFragment();

            Bundle args = new Bundle();
            args.putInt(TabFragment.ARG_TAB_POSITION, position);
            fragment.setArguments(args);

            return fragment;
        }

        @Override
        public int getCount() {
            return tabsList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabsList.get(position).getTitle();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
            FragmentManager manager = ((Fragment)object).getFragmentManager();
            android.support.v4.app.FragmentTransaction trans = manager.beginTransaction();
            trans.remove((Fragment)object);
            trans.commit();
        }
    }

My remove method (called from within the fragment):

public void removeTab() {
            mTabTableHandler.deleteTab(tab.getId()); //db
            tabsList.remove(tabPosition); //data source
            actionBar.removeTabAt(tabPosition); // actionbar

            getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit(); // support fragmentmanager

            tabsList = mTabTableHandler.query(); //requery db
            mSectionsPagerAdapter.notifyDataSetChanged(); //notify adapter
        }

Appreciate the help!

like image 358
Lior Iluz Avatar asked Nov 19 '12 11:11

Lior Iluz


People also ask

How do I delete something from ViewPager?

True you need to implement the getItemPosition() method of your Adaptor but you should override it as intended. The getItemPosition() method tells the ViewPager whether or not to delete viewItem, to redraw or not redraw viewItem when the adapter.

How add and remove fragments?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();

Is ViewPager deprecated?

setOnPageChangeListener (listener: ViewPager. OnPageChangeListener!) This function is deprecated.


1 Answers

you can find the answer here - Remove Fragment Page from ViewPager in Android

it seems what missing in your code is overwriting of the following adapter's method:

@Override
public int getItemPosition(Object object){
    return PagerAdapter.POSITION_NONE;
}

explanation why such "weird" solution works you can find in the answer to the question in the link I provided you.

like image 176
Tal Kanel Avatar answered Sep 23 '22 08:09

Tal Kanel