Android 片段A通知片段B
我在FragA中有一个函数1,在FragB中有一个函数2 我想要的是:当FragA使用function1时,它应该通知FragB使用functionB 那么,什么是最简单、最好的方法呢Android 片段A通知片段B,android,fragment,notify,Android,Fragment,Notify,我在FragA中有一个函数1,在FragB中有一个函数2 我想要的是:当FragA使用function1时,它应该通知FragB使用functionB 那么,什么是最简单、最好的方法呢 感谢func1使用FragmentManager.findFragmentByTag(或byId)查找B片段,然后对该片段调用函数 或者,您可以使用第三方甚至总线,例如func1中的,使用FragmentManager.findFragmentByTag(或byId)查找B片段,然后对该片段调用函数 或者您可以使
感谢func1使用FragmentManager.findFragmentByTag(或byId)查找B片段,然后对该片段调用函数
或者,您可以使用第三方甚至总线,例如func1中的,使用FragmentManager.findFragmentByTag(或byId)查找B片段,然后对该片段调用函数
或者您可以使用第三方甚至总线,如1。使用事件总线 使用EventBus将事件发送到另一个片段。你可以试试看 2。使用FragmentManager和公共方法(我不喜欢这些) 正如@Alexander所建议的,您可以获取对另一个片段的引用并调用其方法 3。使用广播
您可以使用LocalBroadcast并在两个片段中附加一个接收器,以便在它们接收广播时调用该方法。1。使用事件总线 使用EventBus将事件发送到另一个片段。你可以试试看 2。使用FragmentManager和公共方法(我不喜欢这些) 正如@Alexander所建议的,您可以获取对另一个片段的引用并调用其方法 3。使用广播
您可以使用LocalBroadcast并在两个片段中附加一个接收器,以便在它们接收广播时调用该方法。下面是您的问题的示例,虽然它的代码非常粗糙,但可以让您了解如何实现您的需求 您的活动应该如下FragmentTestOneActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import test.spinner.one.FragmentA;
import test.spinner.one.FragmentB;
public class FragmentTestOneActivity extends AppCompatActivity implements FragmentA.MyEventMaker{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_test_one);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public void sendData(String text) {
Log.d("FragmentTestOneActivity", "Text: "+text);
FragmentB.setData(text);
}
}
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* A simple {@link Fragment} subclass.
*/
public class FragmentA extends Fragment {
private MyEventMaker myEventMaker;
private Button button;
public FragmentA() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
myEventMaker = (MyEventMaker)context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_, container, false);
button = (Button) view.findViewById(R.id.FragmentA_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myEventMaker.sendData("something");
}
});
return view;
}
public interface MyEventMaker{
void sendData(String text);
}
}
下面是上述活动的布局活动_片段_测试_one.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cf5f5b"
android:orientation="horizontal"
android:padding="10dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="some.fragment.one.FragmentTestOneActivity"
tools:showIn="@layout/activity_fragment_test_one">
<fragment
android:id="@+id/fragment_one"
android:name="test.spinner.one.FragmentA"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
tools:layout="@layout/fragment_" />
<fragment
android:id="@+id/fragment_two"
android:name="test.spinner.one.FragmentB"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
tools:layout="@layout/fragment_b" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#b1a2f3"
tools:context="test.spinner.one.FragmentA">
<Button
android:id="@+id/FragmentA_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#efabca"
android:gravity="center"
tools:context="test.spinner.one.FragmentB">
<TextView
android:id="@+id/FragmentB_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cf5f5b"
android:orientation="horizontal"
android:padding="10dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="some.fragment.one.FragmentTestOneActivity"
tools:showIn="@layout/activity_fragment_test_one">
<fragment
android:id="@+id/fragment_one"
android:name="test.spinner.one.FragmentA"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
tools:layout="@layout/fragment_" />
<fragment
android:id="@+id/fragment_two"
android:name="test.spinner.one.FragmentB"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
tools:layout="@layout/fragment_b" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#b1a2f3"
tools:context="test.spinner.one.FragmentA">
<Button
android:id="@+id/FragmentA_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#efabca"
android:gravity="center"
tools:context="test.spinner.one.FragmentB">
<TextView
android:id="@+id/FragmentB_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
fragment_b.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cf5f5b"
android:orientation="horizontal"
android:padding="10dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="some.fragment.one.FragmentTestOneActivity"
tools:showIn="@layout/activity_fragment_test_one">
<fragment
android:id="@+id/fragment_one"
android:name="test.spinner.one.FragmentA"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
tools:layout="@layout/fragment_" />
<fragment
android:id="@+id/fragment_two"
android:name="test.spinner.one.FragmentB"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
tools:layout="@layout/fragment_b" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#b1a2f3"
tools:context="test.spinner.one.FragmentA">
<Button
android:id="@+id/FragmentA_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#efabca"
android:gravity="center"
tools:context="test.spinner.one.FragmentB">
<TextView
android:id="@+id/FragmentB_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
以下是您问题的示例,虽然它的代码非常粗糙,但可以让您了解如何实现您的需求 您的活动应该如下FragmentTestOneActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import test.spinner.one.FragmentA;
import test.spinner.one.FragmentB;
public class FragmentTestOneActivity extends AppCompatActivity implements FragmentA.MyEventMaker{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_test_one);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public void sendData(String text) {
Log.d("FragmentTestOneActivity", "Text: "+text);
FragmentB.setData(text);
}
}
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* A simple {@link Fragment} subclass.
*/
public class FragmentA extends Fragment {
private MyEventMaker myEventMaker;
private Button button;
public FragmentA() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
myEventMaker = (MyEventMaker)context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_, container, false);
button = (Button) view.findViewById(R.id.FragmentA_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myEventMaker.sendData("something");
}
});
return view;
}
public interface MyEventMaker{
void sendData(String text);
}
}
下面是上述活动的布局活动_片段_测试_one.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cf5f5b"
android:orientation="horizontal"
android:padding="10dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="some.fragment.one.FragmentTestOneActivity"
tools:showIn="@layout/activity_fragment_test_one">
<fragment
android:id="@+id/fragment_one"
android:name="test.spinner.one.FragmentA"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
tools:layout="@layout/fragment_" />
<fragment
android:id="@+id/fragment_two"
android:name="test.spinner.one.FragmentB"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
tools:layout="@layout/fragment_b" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#b1a2f3"
tools:context="test.spinner.one.FragmentA">
<Button
android:id="@+id/FragmentA_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#efabca"
android:gravity="center"
tools:context="test.spinner.one.FragmentB">
<TextView
android:id="@+id/FragmentB_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cf5f5b"
android:orientation="horizontal"
android:padding="10dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="some.fragment.one.FragmentTestOneActivity"
tools:showIn="@layout/activity_fragment_test_one">
<fragment
android:id="@+id/fragment_one"
android:name="test.spinner.one.FragmentA"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
tools:layout="@layout/fragment_" />
<fragment
android:id="@+id/fragment_two"
android:name="test.spinner.one.FragmentB"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
tools:layout="@layout/fragment_b" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#b1a2f3"
tools:context="test.spinner.one.FragmentA">
<Button
android:id="@+id/FragmentA_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#efabca"
android:gravity="center"
tools:context="test.spinner.one.FragmentB">
<TextView
android:id="@+id/FragmentB_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
fragment_b.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cf5f5b"
android:orientation="horizontal"
android:padding="10dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="some.fragment.one.FragmentTestOneActivity"
tools:showIn="@layout/activity_fragment_test_one">
<fragment
android:id="@+id/fragment_one"
android:name="test.spinner.one.FragmentA"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
tools:layout="@layout/fragment_" />
<fragment
android:id="@+id/fragment_two"
android:name="test.spinner.one.FragmentB"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
tools:layout="@layout/fragment_b" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#b1a2f3"
tools:context="test.spinner.one.FragmentA">
<Button
android:id="@+id/FragmentA_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#efabca"
android:gravity="center"
tools:context="test.spinner.one.FragmentB">
<TextView
android:id="@+id/FragmentB_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
您尝试过什么?您是否听说过接口
?您尝试过什么?您听说过接口
?