首页 > 解决方案 > 使用来自另一个 Fragment 的方法/函数

问题描述

这就是我想要做的,我有一个抽屉布局,里面有框架布局并显示一个片段,这个片段有另一个片段2的框架布局。

所以我希望这个 Fragment2 在单击按钮时使用或激活第一个 Fragment 中的函数,因为我正在将 Fragment 2 切换到 Fragment 3 等等,并且该函数在第一个 Fragment 内。

所以 MainActivity > MainFragment > (Fragment1, Fragment2, etc)。

我总是看到的是 MainActivity > (Fragment1, Fragment2)。

所以我的切换方法是在MainFragment里面,Fragment1,2会用它来替换自己。

我做不到。我已经尝试使用此代码,但它不起作用,

bookingFragment = (BookingFragment) getFragmentManager().findFragmentById(R.id.fragment_booking);

和错误响应一直说我试图读取一个空对象。

主要的:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    tools:context=".Modules.Activities.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bg_tan"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_gravity="center"
                android:src="@drawable/img_logo_header"/>
        </android.support.v7.widget.Toolbar>

        <FrameLayout
            android:id="@+id/main_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="#FFF"
        app:menu="@menu/drawer_view"
        app:headerLayout="@layout/nav_header"  />

</android.support.v4.widget.DrawerLayout>

第一个片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_booking"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_tan"
    android:orientation="vertical"
    tools:context=".Modules.Fragments.BookingFragment">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/layout_status"
            android:layout_marginTop="3dp"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/iv_book_status_address"
                android:layout_weight="1"
                android:background="@drawable/one_orange"
                android:layout_width="0dip"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/iv_book_status_recipient"
                android:layout_weight="1"
                android:background="@drawable/two_gray"
                android:layout_width="0dip"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/iv_book_status_package"
                android:layout_weight="1"
                android:background="@drawable/three_gray"
                android:layout_width="0dip"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_marginTop="3dp"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tv_book_status_address"
                android:layout_weight="1"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:paddingVertical="@dimen/_5sdp"
                android:text="Address"
                android:textColor="@color/colorPrimaryDark"/>
            <TextView
                android:id="@+id/tv_book_status_recipient"
                android:layout_weight="1"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:paddingVertical="@dimen/_5sdp"
                android:text="Recipient"
                android:textColor="@color/hint"/>
            <TextView
                android:id="@+id/tv_book_status_package"
                android:layout_weight="1"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:paddingVertical="@dimen/_5sdp"
                android:text="Packaging"
                android:textColor="@color/hint"/>
        </LinearLayout>

        <FrameLayout
            android:layout_below="@id/layout_status"
            android:id="@id/container"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"/>

    </RelativeLayout>
</LinearLayout>

片段2:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_tan"
    tools:context=".Modules.Fragments.BookAddressFragment">
    <fragment
        android:id="@+id/map_address"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        map:cameraZoom="17"/>

    <LinearLayout
        android:layout_above="@id/layout_address"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_35sdp">

        <Button
            android:id="@+id/btn_on_demand"
            android:layout_marginHorizontal="@dimen/_5sdp"
            android:background="@drawable/btn_white"
            android:elevation="10dp"
            android:layout_weight="1"
            android:layout_width="0dip"
            android:layout_height="@dimen/_30sdp"
            android:text="On Demand"
            android:textColor="@color/hint"
            android:textAllCaps="false"/>

        <Button
            android:id="@+id/btn_next_day"
            android:layout_marginHorizontal="@dimen/_5sdp"
            android:background="@drawable/btn_white"
            android:elevation="10dp"
            android:layout_weight="1"
            android:layout_width="0dip"
            android:layout_height="@dimen/_30sdp"
            android:text="Next Day"
            android:textColor="@color/hint"
            android:textAllCaps="false"/>

        <Button
            android:id="@+id/btn_2_3_days"
            android:layout_marginHorizontal="@dimen/_5sdp"
            android:background="@drawable/btn_white"
            android:elevation="10dp"
            android:layout_weight="1"
            android:layout_width="0dip"
            android:layout_height="@dimen/_30sdp"
            android:text="2-3 Days"
            android:textColor="@color/hint"
            android:textAllCaps="false"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_address"
        android:background="@drawable/btn_white"
        android:layout_alignParentBottom="true"
        android:layout_margin="@dimen/_5sdp"
        android:elevation="10dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/_5sdp"
            android:text="Single Booking"
            android:textColor="@color/hint"/>

        <LinearLayout
            android:gravity="center_vertical"
            android:layout_marginHorizontal="@dimen/_5sdp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_margin="@dimen/_5sdp"
                android:background="@drawable/ic_circle_pickup_small"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <EditText
                android:id="@+id/et_book_address_pickup"
                android:background="@drawable/border_round_gray"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_35sdp"
                android:layout_marginVertical="@dimen/_5sdp"
                android:paddingHorizontal="@dimen/_10sdp"
                android:textColorHint="@color/hint"
                android:textColor="@color/blitz_gray"
                android:inputType="text"
                android:hint="Pick up at"
                android:singleLine="true" />
        </LinearLayout>

        <LinearLayout
            android:gravity="center_vertical"
            android:layout_marginHorizontal="@dimen/_5sdp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_margin="@dimen/_5sdp"
                android:background="@drawable/ic_circle_deliver_small"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <EditText
                android:id="@+id/et_book_address_deliver"
                android:background="@drawable/border_round_gray"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_35sdp"
                android:layout_marginVertical="@dimen/_5sdp"
                android:paddingHorizontal="@dimen/_10sdp"
                android:textColorHint="@color/hint"
                android:textColor="@color/blitz_gray"
                android:inputType="text"
                android:hint="Deliver to"
                android:singleLine="true" />
        </LinearLayout>

        <Button
            android:id="@+id/btn_book_address"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_35sdp"
            android:background="@color/blitz_yellow"
            android:text="Enter recipient details"
            android:textAllCaps="false"
            android:textSize="16sp"
            android:textColor="#000" />
    </LinearLayout>
</RelativeLayout>

标签: androidandroid-fragments

解决方案


最有效的方法是使用 BroadcastReceiver,在你的第一个片段中创建一个接收器

public class CustomBroadcastReceiver extends BroadcastReceiver {
 @Override
    OnReceive(Intent intent){
     // do what you need to do
    }
}

IntentFilter filter = new IntentFilter(“filter”);
CustomerBroadcastReceiver receiver = new BroadcastReceiver();

注册它

LocalBroadcastReceiver.getInstance(this).registerReceiver(receiver, filter);

在fragment2中,当你想激活它时,你需要做的就是

LocalBroadcastReceiver.getInstance(this).sendBroadcast(new Intent);

推荐阅读