多个活动的一个片段

Sar*_*nsh 8 android fragment android-activity

我们总是听说过使用一个活动的多个片段.可能相反吗?我很好奇.我们可以为多个活动使用相同的片段.请举一个例子.

Sur*_*gch 4

如何在多个Activity中重用一个Fragment

在此输入图像描述

带有两个按钮的绿色背景是在多个活动之间重复使用的单个片段。

1. 制定你的fragment类和布局

MyFragment.java

import android.support.v4.app.Fragment;

public class MyFragment extends Fragment implements View.OnClickListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View myLayout = inflater.inflate(R.layout.my_fragment_layout, container, false);

        // add click listeners to the buttons in the fragment
        Button buttonOne = myLayout.findViewById(R.id.button_1);
        Button buttonTwo = myLayout.findViewById(R.id.button_2);
        buttonOne.setOnClickListener(this);
        buttonTwo.setOnClickListener(this);

        return myLayout;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_1:
                Toast.makeText(getContext(), "Button One", Toast.LENGTH_SHORT).show();
                break;
            case R.id.button_2:
                Toast.makeText(getContext(), "Button Two", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

my_fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_green_dark"
    android:orientation="vertical">

    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1"/>

    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

2. 将片段添加到您的活动中

活动_蓝色.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"
    android:background="@android:color/holo_blue_dark"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="goToRedActivityButtonClick"
        android:text="Go to red activity"/>

    <!-- reused fragment -->
    <fragment
        android:id="@+id/my_fragment"
        android:name="com.example.onefragmentmultipleactivities.MyFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

活动_red.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"
    android:background="#ff3636"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="goToYellowActivityButtonClick"
        android:text="Go to yellow activity"/>

    <!-- reused fragment -->
    <fragment
        android:id="@+id/my_fragment"
        android:name="com.example.onefragmentmultipleactivities.MyFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

活动_yellow.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"
    android:background="#f9f478"
    android:orientation="vertical">

    <!-- reused fragment -->
    <fragment
        android:id="@+id/my_fragment"
        android:name="com.example.onefragmentmultipleactivities.MyFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

笔记

为简单起见,我们将片段直接添加到 xml 中。您还可以在代码中动态加载片段。请参阅文档以获取相关帮助。