Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 81b89441 authored by Adam Powell's avatar Adam Powell
Browse files

Fix bug 3093591 - add richer control over default tab selection in ActionBar

Added overloads to ActionBar#addTab with control over whether the added tab
will become selected or not. Old versions implemented in terms of the new.

Change-Id: I810c64652bb7e755b81151ce8a2c765266d78a66
parent 5ffcd088
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
@@ -19457,8 +19457,40 @@
>
<parameter name="tab" type="android.app.ActionBar.Tab">
</parameter>
<parameter name="setSelected" type="boolean">
</parameter>
</method>
<method name="addTab"
 return="void"
 abstract="true"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="tab" type="android.app.ActionBar.Tab">
</parameter>
<parameter name="position" type="int">
</parameter>
</method>
<method name="addTab"
 return="void"
 abstract="true"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="tab" type="android.app.ActionBar.Tab">
</parameter>
<parameter name="position" type="int">
</parameter>
<parameter name="setSelected" type="boolean">
</parameter>
</method>
<method name="getCustomNavigationView"
 return="android.view.View"
+21 −1
Original line number Diff line number Diff line
@@ -397,20 +397,40 @@ public abstract class ActionBar {

    /**
     * Add a tab for use in tabbed navigation mode. The tab will be added at the end of the list.
     * If this is the first tab to be added it will become the selected tab.
     *
     * @param tab Tab to add
     */
    public abstract void addTab(Tab tab);

    /**
     * Add a tab for use in tabbed navigation mode. The tab will be added at the end of the list.
     *
     * @param tab Tab to add
     * @param setSelected True if the added tab should become the selected tab.
     */
    public abstract void addTab(Tab tab, boolean setSelected);

    /**
     * Add a tab for use in tabbed navigation mode. The tab will be inserted at
     * <code>position</code>.
     * <code>position</code>. If this is the first tab to be added it will become
     * the selected tab.
     *
     * @param tab The tab to add
     * @param position The new position of the tab
     */
    public abstract void addTab(Tab tab, int position);

    /**
     * Add a tab for use in tabbed navigation mode. The tab will be insterted at
     * <code>position</code>.
     *
     * @param tab The tab to add
     * @param position The new position of the tab
     * @param setSelected True if the added tab should become the selected tab.
     */
    public abstract void addTab(Tab tab, int position, boolean setSelected);

    /**
     * Remove a tab from the action bar. If the removed tab was selected it will be deselected
     * and another tab will be selected if present.
+22 −11
Original line number Diff line number Diff line
@@ -255,7 +255,6 @@ public class ActionBarImpl extends ActionBar {

    private void configureTab(Tab tab, int position) {
        final TabImpl tabi = (TabImpl) tab;
        final boolean isFirstTab = mTabs.isEmpty();
        final ActionBar.TabListener callback = tabi.getCallback();

        if (callback == null) {
@@ -265,26 +264,38 @@ public class ActionBarImpl extends ActionBar {
        tabi.setPosition(position);
        mTabs.add(position, tabi);

        if (isFirstTab) {
            final FragmentTransaction trans = mActivity.getFragmentManager().openTransaction();
            mSelectedTab = tabi;
            callback.onTabSelected(tab, trans);
            if (!trans.isEmpty()) {
                trans.commit();
            }
        final int count = mTabs.size();
        for (int i = position + 1; i < count; i++) {
            mTabs.get(i).setPosition(i);
        }
    }

    @Override
    public void addTab(Tab tab) {
        mActionView.addTab(tab);
        configureTab(tab, mTabs.size());
        addTab(tab, mTabs.isEmpty());
    }

    @Override
    public void addTab(Tab tab, int position) {
        mActionView.addTab(tab, position);
        addTab(tab, position, mTabs.isEmpty());
    }

    @Override
    public void addTab(Tab tab, boolean setSelected) {
        mActionView.addTab(tab, setSelected);
        configureTab(tab, mTabs.size());
        if (setSelected) {
            selectTab(tab);
        }
    }

    @Override
    public void addTab(Tab tab, int position, boolean setSelected) {
        mActionView.addTab(tab, position, setSelected);
        configureTab(tab, position);
        if (setSelected) {
            selectTab(tab);
        }
    }

    @Override
+4 −6
Original line number Diff line number Diff line
@@ -468,22 +468,20 @@ public class ActionBarView extends ViewGroup {
        return tabView;
    }

    public void addTab(ActionBar.Tab tab) {
    public void addTab(ActionBar.Tab tab, boolean setSelected) {
        ensureTabsExist();
        final boolean isFirst = mTabLayout.getChildCount() == 0;
        View tabView = createTabView(tab);
        mTabLayout.addView(tabView);
        if (isFirst) {
        if (setSelected) {
            tabView.setSelected(true);
        }
    }

    public void addTab(ActionBar.Tab tab, int position) {
    public void addTab(ActionBar.Tab tab, int position, boolean setSelected) {
        ensureTabsExist();
        final boolean isFirst = mTabLayout.getChildCount() == 0;
        final TabView tabView = createTabView(tab);
        mTabLayout.addView(tabView, position);
        if (isFirst) {
        if (setSelected) {
            tabView.setSelected(true);
        }
    }