Skip to content Skip to sidebar Skip to footer

Switch Animation Between Fragments Using Fragmenttabhost

I'm using support.v4 FragmentTabHost, I'm following this sample wich it works: https://github.com/ClareZhang/Android-FragmentTabHost-demo I would like to put an slide animation bet

Solution 1:

One year later, but I just stumbled over the same problem. You're right, the FragmentTabHost takes care of everything, so you can't set the animations from outside.

Here is the original code of the FragmentTabHost which does the tab change:

private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
    TabInfo newTab = null;
    for (int i=0; i<mTabs.size(); i++) {
        TabInfo tab = mTabs.get(i);
        if (tab.tag.equals(tabId)) {
            newTab = tab;
        }
    }
    if (newTab == null) {
        thrownew IllegalStateException("No tab known for tag " + tabId);
    }
    if (mLastTab != newTab) {
        if (ft == null) {
            ft = mFragmentManager.beginTransaction();
        }
        if (mLastTab != null) {
            if (mLastTab.fragment != null) {
                ft.detach(mLastTab.fragment);
            }
        }
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(mContext,
                        newTab.clss.getName(), newTab.args);
                ft.add(mContainerId, newTab.fragment, newTab.tag);
            } else {
                ft.attach(newTab.fragment);
            }
        }

        mLastTab = newTab;
    }
    return ft;
}

My solution was to define five new members like

privateint mAnimationResToRightEnter = -1;
privateint mAnimationResToRightExit = -1;
privateint mAnimationResToLeftEnter = -1;
privateint mAnimationResToLeftExit = -1;
privateint mCurrentTabIndex;

and a public set method like

publicvoidsetAnimation(int animResToRightEnter, int animResToRightExit, int animResToLeftEnter, int animResToLeftExit){
    mAnimationResToRightEnter = animResToRightEnter;
    mAnimationResToRightExit = animResToRightExit;
    mAnimationResToLeftEnter = animResToLeftEnter;
    mAnimationResToLeftExit = animResToLeftExit;
}

Then you can just set the animations in the doTabChanged method before the actual change is done:

private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
    TabInfo newTab = null;
    for (int i=0; i<mTabs.size(); i++) {
        TabInfo tab = mTabs.get(i);
        if (tab.tag.equals(tabId)) {
            newTab = tab;
        }
    }
    if (newTab == null) {
        thrownew IllegalStateException("No tab known for tag " + tabId);
    }
    if (mLastTab != newTab) {
        if (ft == null) {
            ft = mFragmentManager.beginTransaction();

            if (mAnimationResToRightEnter != -1 && mAnimationResToRightExit != -1 &&
                    mAnimationResToLeftEnter != -1 && mAnimationResToLeftExit != -1){
                // The user has set animation resources, let's set the right onesif (getCurrentTab() > m_CurrentTabIndex) {
                    // New tab is on the right, we want to exit and enter to the left
                    ft.setCustomAnimations(mAnimationResToLeftEnter, mAnimationResToLeftExit);
                } else {
                    // New tab is on the left, we want to exit and enter to the right
                    ft.setCustomAnimations(mAnimationResToRightEnter, mAnimationResToRightExit);
                }
                m_CurrentTabIndex = getCurrentTab();
            }
        }


        if (mLastTab != null) {
            if (mLastTab.fragment != null) {
                ft.detach(mLastTab.fragment);
            }
        }
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(mContext,
                        newTab.clss.getName(), newTab.args);
                newTab.fragment.setAllowEnterTransitionOverlap(false);
                newTab.fragment.setAllowReturnTransitionOverlap(false);
                ft.add(mContainerId, newTab.fragment, newTab.tag);
            } else {
                ft.attach(newTab.fragment);
            }
        }

        mLastTab = newTab;
    }
    return ft;
}

Post a Comment for "Switch Animation Between Fragments Using Fragmenttabhost"