切换动画使用FragmentTabHost片段之间

切换动画使用FragmentTabHost片段之间

本文介绍了切换动画使用FragmentTabHost片段之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用support.v4 FragmentTabHost,我下面这个样本至极它的工作原理:

I'm using support.v4 FragmentTabHost, I'm following this sample wich it works:

我想提出片段之间的幻灯片动画,但我无法得到它,我已经使用FragmentTransactions尝试的东西,它不会在所有的工作,我甚至不知道这是正确的的方式,因为FragmentTabHost完成所有的操作:

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();
        }
}

});

有没有简单的方法来实现这一目标用FragmentTabHost?我应该使用TabHost呢?

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

感谢您。

推荐答案

一年过去了,但我只是绊了同样的问题。你说得对,在FragmentTabHost照顾一切,所以你不能从外部设置的动画。

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.

下面是一个标签改变FragmentTabHost原来的code:

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;

和像

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

然后,你可以设定动画的doTabChanged方法的实际变化之前完成:

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;
}

这篇关于切换动画使用FragmentTabHost片段之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:51