我正在使用support.v4 FragmentTabHost,下面是它起作用的示例:
https://github.com/ClareZhang/Android-FragmentTabHost-demo
我想在片段之间放一张幻灯片动画,但我无法获得它,我已经使用FragmentTransactions尝试了一些操作,但它根本不起作用,而且我什至不确定这是否是正确的方法,因为FragmentTabHost照顾一切:
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吗?
谢谢。
最佳答案
一年后,但我偶然发现了同样的问题。没错,FragmentTabHost可以处理所有事情,因此您不能从外部设置动画。
这是FragmentTabHost的原始代码,它会更改选项卡:
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;
}
我的解决方案是定义五个新成员,例如
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方法中设置动画:
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;
}