2016-03-13 79 views
0

我一直在試圖讓我的浮動動作按鈕在我的片段中工作,它切換到哪裏,併爲下一個片段做滑動動畫,但由於某種原因,當我點擊浮動動作按鈕,它什麼都不做。有人能幫我指出原因嗎?安卓 - 按鈕不會切換片段

這裏是我的片段類

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

/** 
* A simple {@link Fragment} subclass. 
*/ 
public class ScoutFragment extends Fragment { 

    FloatingActionButton addDataScout; 

    public ScoutFragment() { 
     // Required empty public constructor 
    } //End of ScoutFragment 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_scout, container, false); 

     //Setup Floating Action Button 
     FloatingActionButton addDataScout = (FloatingActionButton) view.findViewById(R.id.fab); 
     addDataScout.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       AddScoutDataFragment fragment = new AddScoutDataFragment(); 
       FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
       fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left); 
       fragmentTransaction.replace(R.id.fragment_container, fragment); 
       fragmentTransaction.commit(); 
      } //End of onClick 
     }); //End of setOnClickListener 

     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_scout, container, false); 
    } //End of onCreateView 
} //End of class 

這裏是xml文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.compscitutorials.basigarcia.ramfernoscout.ScoutFragment"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab" 
      android:layout_width="61dp" 
      android:layout_height="57dp" 
      android:src="@mipmap/ic_action_add" 
      app:backgroundTint="#F28016" 
      app:elevation="5dp" 
      app:borderWidth="0dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentEnd="true" 
      android:layout_marginBottom="12dp" 
      android:layout_marginRight="10dp" 
      android:scaleType="fitCenter" 
      android:clickable="true"> 
     </android.support.design.widget.FloatingActionButton> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Western University" 
      android:id="@+id/testText" 
      android:layout_centerInParent="true" 
      android:textSize="24dp" 
      android:textStyle="bold" /> 
    </RelativeLayout> 
</FrameLayout> 

我也使用NavigationDrawer爲我的項目

+0

你可以這樣做: [FragmentTransaction inside a Fragment](http://stackoverflow.com/a/15504850/5124783) –

+0

你可以查看我的評論。 –

回答

2

你給容器名稱爲fragment_container但在哪裏呢?你必須做的是給幀FrameLayout id爲fragment_container

android:id="@+id/fragment_container"添加到FrameLayout,然後您就可以開始了。

+0

我將ID更改爲'android:id =「@ + id/layoutScout」'然後將代碼更改爲'fragmentTransaction.replace(R.id.layoutScout,fragment);'但沒有任何改變,我點擊按鈕和其他片段不會彈出 –

+0

@Samer Alabi更改'返回inflater.inflate(R.layout.fragment_scout,容器,false);' 與'返回視圖;'爲什麼你膨脹兩次? –

+0

這有效,另一個片斷彈出,但另一個仍然存在,我該怎麼辦才能解決這個問題 –

0

是的!它什麼也不做,因爲:

  • R.id.data_add不能fragment_scout

片段交易的replace方法來發現將取代任何視圖容器(R.id.data_add),但是FragmentTransaction不管理集裝箱。

修復:

  • 移動的替代作用,以片段的父視圖這樣suggestion
+0

檢查更新的文章 –