這是@ Geoff的出色答案的後續,但適合更加動態和真實的場景。
我想這是一個很好的小貼子,但我現在意識到它有一點失控。但是,代碼就在那裏,我發現它非常有用,不過它涵蓋的不僅僅是如何禁用過渡動畫。
通常,當我使用片段時,我喜歡將BaseFragment附加到BaseActivityCallback上。這BaseActivityCallback可以通過我的碎片可以用來在上面添加一個新片段的本身,甚至彈出的碎片在其下方,因此希望禁用彈出動畫 - 或彈出默默:
interface BaseActivityCallback
{
void addFragment (BaseFragment f, int containerResId);
void popFragment (boolean silently);
}
class BaseActivity extends android.support.v4.app.FragmentActivity implements BaseActivityCallback
{
public void addFragment (BaseFragment f, int containerResId)
{
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.enter, R.anim.pop_exit); // http://stackoverflow.com/a/17488542/2412477
ft.addToBackStack(DEFAULT_FRAGMENT_STACK_NAME);
ft.replace(containerResId, fragment);
ft.commitAllowingStateLoss();
}
public void popFragment (boolean silently)
{
FragmentManager fm = getSupportFragmentManager();
if (silently) {
int count = fm.getFragments().size();
BaseFragment f = (BaseFragment)fm.getFragments().get(count-1);
f.setDisableTransitionAnimations(true);
}
fm.popBackStackImmediate();
}
}
public abstract class BaseFragment extends android.support.v4.app.Fragment
{
private static final String TAG = "BaseFragment";
private final String STATE_DISABLE_TRANSITION_ANIMATIONS = TAG+".stateDisableTransitionAnimations";
protected BaseActivityCallback baseActivityCallback;
private boolean disableTransitionAnimations;
@Override
public void onCreate (@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
disableTransitionAnimations = (savedInstanceState==null ? false : savedInstanceState.getBoolean(STATE_DISABLE_TRANSITION_ANIMATIONS, false));
}
@Override
public void onAttach (Context context)
{
super.onAttach(context);
baseActivityCallback = (BaseActivityCallback)context;
}
@Override
public void onSaveInstanceState (Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putBoolean(STATE_DISABLE_TRANSITION_ANIMATIONS, disableTransitionAnimations);
}
@Override
public Animation onCreateAnimation (int transit, boolean enter, int nextAnim)
{
if (disableTransitionAnimations) {
Animation nop = new Animation(){};
nop.setDuration(0);
return nop;
}
return super.onCreateAnimation(transit, enter, nextAnim);
}
public void setDisableTransitionAnimations (boolean disableTransitionAnimations)
{
this.disableTransitionAnimations = disableTransitionAnimations; // http://stackoverflow.com/a/11253987/2412477
}
}
現在你可以創建你MainActivity
,並有顯示Fragment1
可以添加其他Fragment2
這又會彈出Fragment1
默默:
public class MainActivity extends BaseActivity
{
protected void onCreate (Bundle savedInstanceState)
{
setContentView(R.layout.main_activity);
...
if (getSupportFragmentManager().getFragments() != null && !getSupportFragmentManager().getFragments().isEmpty()) {
addFragment(FragmentA.newInstance(), R.id.main_activity_fragment_container);
}
}
...
}
public class FragmentA extends BaseFragment
{
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
ViewGroup root = (ViewGroup)inflater.inflate(R.layout.fragment_a, container, false);
...
root.findViewById(R.id.fragment_a_next_button)
.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
baseActivityCallback.addFragment(FragmentB.newInstance(), R.id.main_activity_fragment_container);
}
});
}
}
public class FragmentB extends BaseFragment
{
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
ViewGroup root = (ViewGroup)inflater.inflate(R.layout.fragment_b, container, false);
...
root.findViewById(R.id.fragment_b_pop_silently_button)
.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
baseActivityCallback.popFragment(true);
}
});
}
}
我剛剛從其他活動返回時表示什麼意思?你能否告訴過渡步驟,即你如何嘗試導航。 – 500865 2012-02-16 18:13:56
嗨500865,我給這個問題添加了一個示例導航。 – ChristophK 2012-02-17 08:46:25
不會將setCustomAnimations中的第3個和第4個參數設置爲0嗎? – Boy 2012-10-11 05:45:23