Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在应用程序启动时加载片段_Java_Android_Android Fragments - Fatal编程技术网

Java 如何在应用程序启动时加载片段

Java 如何在应用程序启动时加载片段,java,android,android-fragments,Java,Android,Android Fragments,我已经创建了一个应用程序,它基本上有导航抽屉,我希望在活动启动时加载一个片段“home”,而不是主活动。 基本解决方案可以是在您的onCreate方法中实现如下代码 // get fragment manager FragmentManager fm = getFragmentManager(); // replace FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.main_layout, new HomeFra

我已经创建了一个应用程序,它基本上有导航抽屉,我希望在活动启动时加载一个片段“home”,而不是主活动。
基本解决方案可以是在您的
onCreate
方法中实现如下代码

// get fragment manager
FragmentManager fm = getFragmentManager();

// replace
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.main_layout, new HomeFragment());
ft.commit();

您可以调用此方法来查看片段。在您的情况下,在
onCreate()上调用此方法

示例用法

 changeFragment(new YourFragment());
您的活动:

public class MainActivity extends AppCompatActivity {
    public static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
    }

    public String getHelloMessage() {
        return "Hello!";
    }
}
你的看法:

public class MainView extends Fragment {

    // Declarations
    private Button testButton;

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

        // Getting the reference of controller from Application
        MainController mainController = Application.getInstance().getMainController();

        // Initializing view objects
        testButton = view.findViewById(R.id.test_button);

        // Setting actions
        testButton.setOnClickListener(mainController.getTestAction());

        return view;
    }

    // Reference to the view Object
    public Button getTestButton() {
        return testButton;
    }
您的main_activity.xm lto用于连接片段

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/containerMainView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/mainView"
        android:name="com.template.views.MainView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

主_view.xml文件是最终的视图定义

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/test_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/say_hello" />
</RelativeLayout>

片段是活动的内容,您不能加载它们。不过,您可以在布局中设置它们
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/test_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/say_hello" />
</RelativeLayout>