Java 安卓-按钮赢了';t开关片段
我一直在尝试让我的浮动动作按钮在片段中工作,它会切换到下一个片段,并将动画滑动到下一个片段,但由于某种原因,当我单击浮动动作按钮时,它什么也不做。有人能帮我指出原因吗 这是我的片段类Java 安卓-按钮赢了';t开关片段,java,android,android-fragments,button,Java,Android,Android Fragments,Button,我一直在尝试让我的浮动动作按钮在片段中工作,它会切换到下一个片段,并将动画滑动到下一个片段,但由于某种原因,当我单击浮动动作按钮时,它什么也不做。有人能帮我指出原因吗 这是我的片段类 import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.Fragment; import android.support.v4.app.Fra
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
是的!它什么也不做,因为:
- 在
fragment\u scout
replace
方法将替换容器中的任何视图(R.id.data\u add),但是FragmentTransaction
不管理容器
修正:
- 将替换操作移动到片段的父视图,如下所示
- 在
fragment\u scout
是的!它什么也不做,因为:
replace
方法将替换容器中的任何视图(R.id.data\u add),但是FragmentTransaction
不管理容器
修正:
- 将替换操作移动到片段的父视图,如下所示
fragment\u container
,但它在哪里?您需要做的是将FrameLayout的id指定为fragment\u container
将
android:id=“@+id/fragment\u container”
添加到框架布局中,您就可以开始了。您将容器名称命名为fragment\u container
,但它在哪里?您需要做的是将FrameLayout的id指定为fragment\u container
将
android:id=“@+id/fragment\u container”
添加到FrameLayout,您就可以开始了。我将id更改为android:id=“@+id/layoutScout”
然后将代码更改为fragmentTransaction.replace(R.id.layoutScout,fragment)代码>但是没有任何改变,我点击按钮,另一个片段没有弹出up@Samer阿拉比更改返回充气机。充气(R.layout.fragment\u scout,容器,假)代码>带有返回视图代码>为什么要膨胀两次?这有点奏效,另一个片段弹出,但另一个仍然存在,我该怎么做才能修复that@SamerAlabi尝试在onCreate
方法中调用container.removeallview()
。既然我的解决方案奏效了,你能接受我的回答吗?像这样?(对于Android开发来说真的很陌生,抱歉)public void onCreate(ViewGroup容器,Bundle savedInstanceState){container.removeAllViews();}//onCreate的结束
我将id更改为Android:id=“@+id/layoutScout”
然后将代码更改为fragmentTransaction.replace(R.id.layoutScout,fragment)代码>但是没有任何改变,我点击按钮,另一个片段没有弹出up@Samer阿拉比更改返回充气机。充气(R.layout.fragment\u scout,容器,假)代码>带有返回视图代码>为什么要膨胀两次?这有点奏效,另一个片段弹出,但另一个仍然存在,我该怎么做才能修复that@SamerAlabi尝试在onCreate
方法中调用container.removeallview()
。既然我的解决方案奏效了,你能接受我的回答吗?像这样?(对于Android开发来说真的是新手对不起)public void onCreate(ViewGroup容器,Bundle savedInstanceState){container.removeAllViews();}//onCreate的结尾
你可以这样做:你可以检查我的评论。你可以这样做:你可以检查我的评论。