一个片段用于多种活动 [英] One fragment for multiple activities

查看:71
本文介绍了一个片段用于多种活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们一直听说使用多个片段进行一项活动.有可能相反吗?我对此感到很好奇.我们可以将相同的片段用于多个活动吗?请举一个例子.

We always have heard using multiple fragments with one activity. Is opposite possible? I am curious about this. Can we use same fragment for multiple activities. Please give ONE EXAMPLE.

推荐答案

如何在多个活动中重用一个片段

带有两个按钮的绿色背景是一个片段,可以在多个活动中重复使用.

The green background with two buttons is a single fragment that is reused among multiple activities.

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中.您还可以在代码中动态加载片段.请参阅文档以获得帮助.

这篇关于一个片段用于多种活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆