Switch Animation between fragments using FragmentTabHost

自古美人都是妖i 提交于 2020-01-25 03:49:27

问题


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 between fragments but I'm unable to get it, I've tried something using FragmentTransactions,it doesn't work at all, and I'm not even sure if this is the right way because FragmentTabHost takes care of everything:

  mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
    Log.v("onTabChanged", tabId);
        FragmentManager manager = getSupportFragmentManager();
        Fragment fr = manager.findFragmentByTag(tabId);
        if(fr != null){
            Log.v("Fragment", "TRANSACTION");
            FragmentTransaction ft  =   manager.beginTransaction();
            ft.setCustomAnimations(R.anim.left_slide_in, R.anim.left_slide_out);
            //ft.replace(R.id.realtabcontent, ...); //don't know how to get next fragment
            ft.commit();
        }
}

});

Is there any simple way to achieve this using FragmentTabHost?, Should I use TabHost instead?

Thank you.


回答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) {
        throw new 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

private int mAnimationResToRightEnter = -1;
private int mAnimationResToRightExit = -1;
private int mAnimationResToLeftEnter = -1;
private int mAnimationResToLeftExit = -1;
private int mCurrentTabIndex;

and a public set method like

public void setAnimation(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) {
        throw new 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 ones
                if (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;
}


来源:https://stackoverflow.com/questions/22761915/switch-animation-between-fragments-using-fragmenttabhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!