我一直在試圖讓我的浮動動作按鈕在我的片段中工作,它切換到哪裏,併爲下一個片段做滑動動畫,但由於某種原因,當我點擊浮動動作按鈕,它什麼都不做。有人能幫我指出原因嗎?安卓 - 按鈕不會切換片段
這裏是我的片段類
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
爲我的項目
你可以這樣做: [FragmentTransaction inside a Fragment](http://stackoverflow.com/a/15504850/5124783) –
你可以查看我的評論。 –