gpt4 book ai didi

java - Android - 按钮不会切换 fragment

转载 作者:行者123 更新时间:2023-12-02 03:49:59 24 4
gpt4 key购买 nike

我一直在尝试让 float 操作按钮在我的 fragment 中工作,切换到下一个 fragment 并执行幻灯片动画,但由于某种原因,当我单击 float 操作按钮时,它什么也不做。谁能帮我指出原因。

这是我的 fragment 类

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_container 但它在哪里?您所要做的是将 id 作为 fragment_container 提供给 FrameLayout。

android:id="@+id/fragment_container" 添加到 FrameLayout 中,然后就可以开始了。

关于java - Android - 按钮不会切换 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35967089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com