【问题标题】:FragmentTabHost onTaChanged animation not workingFragmentTabHost onTaChanged 动画不起作用
【发布时间】:2015-01-30 21:41:38
【问题描述】:

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

但是动画无法启动...我尝试了 startAnimation 但也没有效果...?

这是在我的父片段的 onCreateView 方法中添加的,它具有 mTab​​Host 字段...

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

}

【问题讨论】:

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


    【解决方案1】:

    您不能像在普通 TabHost 中那样在视图上使用动画,因为片段在 FragmentTabHost 内被分离和附加。

    看看possible solution for animations with FragmentTabHost

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-03
      • 2017-08-18
      • 2018-09-27
      • 2011-02-01
      相关资源
      最近更新 更多