FragmentTabHost onTaChanged动画不起作用

时间:2015-01-30 21:41:39

标签: android android-fragments android-support-library fragment-tab-host fragmentmanager

我在标签主机中为片段/标签添加了动画,就像这里一样:https://stackoverflow.com/a/12109268/1865583

但动画才刚开始......我尝试使用startAnimation,但也没有效果......?

这是在我父片段的onCreateView方法中添加的,它有mTabHost字段...

    //On tab changed
    mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabId) {
            logInfo("Tab changed id " + tabId);
            currentView = mTabHost.getCurrentView();
            if (mTabHost.getCurrentTab() > currentTab)
            {

                previousView.setAnimation(outToLeftAnimation());
                currentView.setAnimation(inFromRightAnimation());
            }
            else
            {
                previousView.setAnimation(outToRightAnimation());
                currentView.setAnimation(inFromLeftAnimation());
            }
            previousView = currentView;
            currentTab = mTabHost.getCurrentTab();
        }
    });


 /**
 * Custom animation that animates in from right
 * 
 * @return Animation the Animation object
 */
private Animation inFromRightAnimation()
{
    Animation inFromRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
    return setProperties(inFromRight);
}

/**
 * Custom animation that animates out to the right
 * 
 * @return Animation the Animation object
 */
private Animation outToRightAnimation()
{
    Animation outToRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
    return setProperties(outToRight);
}

/**
 * Custom animation that animates in from left
 * 
 * @return Animation the Animation object
 */
private Animation inFromLeftAnimation()
{
    Animation inFromLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
    return setProperties(inFromLeft);
}

/**
 * Custom animation that animates out to the left
 * 
 * @return Animation the Animation object
 */
private Animation outToLeftAnimation()
{
    Animation outtoLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
    return setProperties(outtoLeft);
}

/**
 * Helper method that sets some common properties
 * @param animation the animation to give common properties
 * @return the animation with common properties
 */
private Animation setProperties(Animation animation)
{
    animation.setDuration(ANIMATION_TIME);
    animation.setInterpolator(new AccelerateInterpolator());
    return animation;
}

}

1 个答案:

答案 0 :(得分:0)

您不能在普通TabHost中的视图上使用动画,因为片段已分离并附加在FragmentTabHost中。

查看possible solution for animations with FragmentTabHost