1

我已經實現該方法的活性:LayoutTransition導致片段不會顯示在創建第二時間

public void pushFragment(MyFragment fragment) { 
    stack.add(fragment); 
    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.replace(R.id.main_frame_layout, fragment); 
    fragmentTransaction.addToBackStack(null); 
    fragmentTransaction.commit(); 
    setTitle(fragment.getTitle(this)); 
} 

在片段,我有此onItemClickListener列表視圖:

getAppActivity().pushFragment(anotherFragment); 

AnotherFragment onCreateView方法:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    setHasOptionsMenu(true); 

    LayoutTransition t = new LayoutTransition(); 
    container.setLayoutTransition(t); 

    View rootView = inflater.inflate(R.layout.another_layout, container, false); 

    final ViewGroup vg = (ViewGroup)rootView.findViewById(R.id.avatar_image_layout); 
    vg.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING); 

    // Stuff 

    return rootView; 
} 

在API級別> = 19上一切正常。 在API 18和17(我沒有在API 16上測試過)中,第一次點擊它的工作列表視圖項。然後我回去再點擊該項目的另一個時間。這次創建片段,調用onActivityCreated和onCreateView等,但不顯示片段。

編輯1: 評論隨機的東西,我發現,這個問題是:

LayoutTransition t = new LayoutTransition(); 
container.setLayoutTransition(t); 

評論這兩條線可以解決問題,但我不知道爲什麼,我失去了動畫。 ..

+0

當你按下後備時,你的代碼是什麼?你重寫它並使用fragment.popbackstack()? –

+0

是的,但正如你可以從我的編輯1看到的,這個問題是其他地方 – andrew

+0

Im避免使用我的片段上的動畫,但如果你想,我想這可能會幫助你http://stackoverflow.com/questions/9194311/pop - 片段backstack - 沒有彈出的流行動畫 –

回答

0

這可能因API而異,但有可能在調用commit()info)後不會立即應用FragmentTransaction

根據FragmentTransaction.java,該commit()法規定:

* Schedules a commit of this transaction. The commit does 
* not happen immediately; it will be scheduled as work on the main thread 
* to be done the next time that thread is ready. 

爲了使這立即發生,commit()方法之後,我們稱之爲FragmentManager.javaexecutePendingTransactions()more info ):

* After a {@link FragmentTransaction} is committed with 
* {@link FragmentTransaction#commit FragmentTransaction.commit()}, it 
* is scheduled to be executed asynchronously on the process's main thread. 
* If you want to immediately executing any such pending operations, you 
* can call this function (only from the main thread) to do so. Note that 
* all callbacks and other related behavior will be done from within this 
* call, so be careful about where this is called from. 
+0

不幸的是,這並沒有解決我的問題... – andrew