Asaf Nevo
Asaf Nevo

Reputation: 11678

Android SupportActionBar tabs disappear under Fragment

I'm using the Support Package v7 for adding Action Bar Tabs to my app.

When the tab's Fragment has a root view with height="fill_parent" the Fragment is covering my tabs.

When I’m changing the Fragment's root view to height="wrap_content" I can see the tab but my Fragment styling is affected.

My code for loading Fragments is the same as in Android Docs:

public class PicoTabListener<T extends Fragment> implements TabListener {

    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /** Constructor used each time a new tab is created.
      * @param activity  The host Activity, used to instantiate the fragment
      * @param tag  The identifier tag for the fragment
      * @param clz  The fragment's Class, used to instantiate the fragment
      */
    public PicoTabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    /* The following are each of the ActionBar.TabListener callbacks */

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        }        
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);
        }
    }

    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
        // do nothing       
    }

Isn't tab should always be in the top of the screen as the first layer?

Upvotes: 0

Views: 555

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006674

Isn't tab should always be in the top of the screen as the first layer ?

No.

If you were using ActionBarSherlock, I think what you are doing would work. And if you use the native action bar implementation, I think what you are doing would work. However, you cannot do this with AppCompat:

ft.add(android.R.id.content, mFragment, mTag);

Use some container inside of your layout, like a FrameLayout, as the target of the transaction, not android.R.id.content. See this issue for more.

Upvotes: 1

Related Questions