2017-07-15 72 views
0

對於我的應用程序的用戶界面,我使用Transition s和Fragment s。在我的第一個FragmentonCreateView函數中,我啓動了一個Transition將顯示從第一個Scene更改爲第二個Scene。之後,當我點擊一個Button時,我開始另一個Transition顯示某種由第三個Scene代表的「splashscreen」,並且在onTransactionEnd中,我嘗試將Fragment更改爲myView.postDelayed(new myRunnableForChangingFragment(),1000)轉換和片段

在調試模式下,我看到我的Runnable已正確啓動,但新的Fragment未替換第一個......但是,確實發生了一些情況:必須單擊後退按鈕上的兩次才能返回到我的第一個Fragment。事實上,它似乎Fragment被激活,但不可見...

任何想法,我怎麼能解決這個問題?

由於要求在第一個評論,這裏是我用來更改代碼的Fragment

fragment=new mySecondFragment(); 
    transaction=getActivity().getFragmentManager().beginTransaction(); 
    transaction.replace(R.id.Conteneur, fragment,"Fragment tag"); 
    transaction.addToBackStack(null); 
    transaction.commit(); 

我可以添加該代碼從onTransitionEnd作品完美地外推出。

爲「容器」中的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/Conteneur" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">  
</LinearLayout> 

用於第一和第二場景中的XML文件(唯一的區別;按鈕中的第一個設置爲不可見的代碼) :

<TextView 
    android:id="@+id/TitreJeux" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginBottom="@dimen/MargeVerticaleBoutonsInterfaceJeux" 
    android:lines="1" 
    android:text="@string/app_name" 
    android:textColor="#FFFF00" 
    android:textSize="35sp" 
    android:textStyle="bold" 
    android:typeface="serif"/> 

<vd63.jeux.JeuAnimationRapide.BoutonInterface 
    android:id="@+id/BoutonOptions" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_gravity="center" 
    android:layout_marginTop="@dimen/MargeVerticaleBoutonsInterfaceJeux" 
    android:layout_weight="1" 
    android:background="@drawable/bouton_jouer" 
    android:text="@string/BoutonOptions" 
    android:textColor="@color/CouleurTexteBoutons" 
    android:textSize="25sp" 
    android:typeface="serif"/> 

的XML FLE的 「飛濺」 場景:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:id="@+id/EcranChargement" 
      android:orientation="vertical" 
      android:gravity="center" 
      android:background="@drawable/fond_interface" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      tools:context=".JeuAnimationRapide"> 

<ImageView 
    android:id="@+id/VD63" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/vd63"/> 

<ImageView 
    android:id="@+id/Chargement" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/chargement"/> 

我過渡從場景中的一個(按鈕不可見的,簡單Slide)到場景2然後返回到場景1(Slide再次)然後到「飛濺」場景(具有Fade轉換)。

+0

?請分享FragmentTransaction代碼? –

+0

好的,問題解決了!我搞亂了不同的觀點和容器...... – Zelig63

回答

1

當你從片段1切換到片段2使用下面的代碼它適用於我我希望它會幫助你。

Fragment fragment = SearchActivity.newInstance(context); 
String tag = fragment.toString(); 
getFragmentManager().beginTransaction() 
.addToBackStack(tag)  
.replace(R.id.fragment, new SearchActivity()) 
.commit(); 

使用addToBackStack當你想回來這個片段再次backpress否則你不需要添加addTobackstack