【问题标题】:One fragment for multiple activities一个片段用于多个活动
【发布时间】:2017-01-16 13:41:33
【问题描述】:

我们一直听说在一个活动中使用多个片段。有可能相反吗?我对此很好奇。我们可以为多个活动使用相同的片段吗?请举一个例子。

【问题讨论】:

  • developer.android.com/guide/components/fragments.html#Design "例如——继续以新闻应用程序为例——当在平板电脑大小的设备上运行时,应用程序可以在 Activity A 中嵌入两个片段。但是,在手机大小的屏幕上, 两个片段都没有足够的空间,所以 Activity A 只包含文章列表的片段,当用户选择文章时,它会启动 Activity B,其中包含第二个片段来阅读文章。”

标签: android android-activity fragment


【解决方案1】:

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

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

1。制作你的片段类和布局

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;
        }
    }
}

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>

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

activity_blue.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>

activity_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>

activity_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>

注意事项

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

【讨论】:

  • 如何从活动类访问片段上的按钮?
  • @Mwas,你可以在片段中有一个公共的 getter 方法。 getter 可以返回对按钮的引用。如果需要发起联系,Activity 可以直接访问此方法。如果片段需要在单击按钮时向活动发送消息,那么您应该使用侦听器接口。有关这两种通信类型的示例,请参阅this answer。可能你想要的是听众。包含该片段的每个活动都将实现侦听器接口。
【解决方案2】:

是的,一个片段可以包含多个活动。 但是您需要使用 LayoutParams 使用 java 对布局进行编程,并将它们嵌入到每个片段实例中。

在每个活动上,您都需要调用此片段 在 Java 中创建您的 UI 组件,从 Java 类(即您的活动)动态地将它们添加到布局中。

如果您对 Java 不是特别熟悉,我建议这种方法不容易维护。对于这种方法,您需要忘记 XML,因为其中根本没有任何内容,一切都将仅使用 Java 类完成。

【讨论】:

  • 为什么需要在 Java 中进行布局?
  • 这样它是完全可移植和动态的。它每次都会在多个地方重新创建相同的片段,因此您必须从一开始就摆脱您的 xml。否则,您将需要为其设置框架布局,并且每次都需要切换布局
【解决方案3】:

generic_error_msg_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/White" >

    <TextView
        android:id="@+id/error_message_textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:textColor="@android:color/darker_gray"
        android:textSize="@dimen/font_size_16sp" />


        <Button
            android:id="@+id/error_button_handler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             android:layout_alignParentBottom="true"
            android:layout_gravity="left|center_vertical"
            android:textSize="@dimen/font_size_16sp"
             />

</RelativeLayout>

GenericErrorFragment.Java

public class GenericErrorFragment extends Fragment{


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }




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


        View genericView = inflater.inflate(R.layout.generic_error_msg_fragment, null);
        TextView errorText = (TextView) genericView.findViewById(R.id.error_message_textview);

        errorText.setText("error msg" );

        Button errorbutton = (Button) genericView.findViewById(R.id.error_button_handler);

        errorbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // your logic launch some other activity

            }
        });
        return genericView;
    }

}

您可以在任何活动中加载此片段,并可以为错误和按钮处理程序定义自定义文本

【讨论】:

    【解决方案4】:

    我不会写完整的代码,但我可以给你你正在寻找的确切例子

    想象一个应用程序,其中一个人可以注册为adminuser

    现在在处理它时,您在 admin registration activity 询问中制作了 3 个片段

    1.) 个人信息

    2.) 学术信息

    3.) 管理员详细信息

    现在 user registration activity 说你制作 2 个片段来获取以下信息

    1.) 个人信息

    2.) 用户详细信息

    您在这里使用个人信息片段 2 次进行 2 次活动

    这个代码是孩子的游戏主要是概念

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      相关资源
      最近更新 更多