Android FragmentTabHost OnTachChanged动画不工作
我在tab host中为片段/选项卡添加了动画,如下所示: 但是动画无法启动…我尝试了startAnimation,但也没有效果 这是在我的父片段的onCreateView方法中添加的,该方法具有mTabHost字段Android FragmentTabHost OnTachChanged动画不工作,android,android-fragments,android-support-library,fragment-tab-host,fragmentmanager,Android,Android Fragments,Android Support Library,Fragment Tab Host,Fragmentmanager,我在tab host中为片段/选项卡添加了动画,如下所示: 但是动画无法启动…我尝试了startAnimation,但也没有效果 这是在我的父片段的onCreateView方法中添加的,该方法具有mTabHost字段 //On tab changed mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChan
//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;
}
}您不能像在普通选项卡主机中那样在视图上使用动画,因为片段是在FragmentTabHost内部分离和附加的 看一看