我有一个活动和2个片段。
我想在活动中点火
listener.receivePreview(obj)
然后
- 执行:OneFragment - > receivePreview。
- 执行:TwoFragment - > receivePreview。
醇>
public class MainAct extends AppCompatActivity {
public interface OnReceiveListener {
// This can be any number of events to be sent to the activity
void receivePreview(Object... obj);
}
private OnReceiveListener listener;
}
public class OneFragment extends Fragment implements OnReceiveListener{
@Override
public void receivePreview(Object... obj) {
}
}
public class TwoFragment extends Fragment implements OnReceiveListener{
@Override
public void receivePreview(Object... obj) {
}
}
答案 0 :(得分:3)
我使用答案Sirvan Paraste。这似乎是一个有用的解决方案。
public class MainAct extends AppCompatActivity {
static List<OnReceiveListener> onReceiveList = new ArrayList<OnReceiveListener>();
public void attachOnReceiveListener(OnReceiveListener listener) {
if (!onReceiveList.contains(listener)) {
onReceiveList.add(listener);
}
}
public interface OnReceiveListener {
// This can be any number of events to be sent to the activity
void receivePreview(Object... obj);
}
public onReceivePreview(Object... obj) {
for (OnReceiveListener listener : onReceiveList) {
listener.receivePreview(obj);
}
}
}
public class OneFragment extends Fragment implements OnReceiveListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainAct mainAct = new MainAct();
mainAct.attachOnReceiveListener(this);
}
@Override
public void receivePreview(Object... obj) {
}
}
public class TwoFragment extends Fragment implements OnReceiveListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainAct mainAct = new MainAct();
mainAct.attachOnReceiveListener(this);
}
@Override
public void receivePreview(Object... obj) {
}
}
答案 1 :(得分:2)
我认为您可以使用观察者模式,这在您的情况下是一种很好的做法。
如GoF所述:
“定义对象之间的一对多依赖关系,以便当一个对象更改状态时,将自动通知并更新其所有依赖项”。
在http://www.java2blog.com/2013/02/observer-design-pattern-in-java.html#TLio7G2ruqxvfZUR.99
了解详情在你的情况下,你有这样的关系(一对多),当你想要知道两个片段的活动中发生事件时。
片段是实现观察者类,您的活动具有主题角色,如上图所示。
我希望这可以帮助您以非常好的方式实现您的代码。 可以在以下链接中找到一些教程:
https://dzone.com/articles/observer-pattern-java http://www.tutorialspoint.com/design_pattern/observer_pattern.htm
编辑:在给定的情况下:
public interface OnReceiveListener { // this is your observer interface !
// This can be any number of events to be sent to the activity
void receivePreview(Object... obj);
}
使用此设计模式正确定义片段,因此我不会更改其代码:
公共类OneFragment扩展Fragment实现OnReceiveListener {
@Override
public void receivePreview(Object... obj) {
}
}
public class TwoFragment extends Fragment implements OnReceiveListener{
@Override
public void receivePreview(Object... obj) {
}
您需要引用活动中的片段(作为观察者)。
ArrayList< OnReceiveListener > observers = new ArrayList< OnReceiveListener>();
确实观察者可以订阅或注册自己的主题(片段持有对活动的引用(最好使用单例模式!:D)。像这样:
public class MainAct extends AppCompatActivity {
private static MainAct instance;
public static MainAct getInstance() {
if(instance != null)
return instance;
}
// I think it is better to create the instance variable in the onCreate() method of the MainAct activity
onCreate(...)
{
.
.
.
instance = this;
...
}
public void registerObserver(OnReceiveListener observer){
observers.add(observer)
}
notifyObservers(){
// call this method in the listener you want
for( Observer obser : observers)
obser. receivePreview(param )
}
...
片段初始化中的//: MainAct.getInstance()。registerObserver(本)
答案 2 :(得分:1)
尝试在每个片段中创建一个返回接口实例
的函数public OnReceiveListener getListener() {
mListener = this;
return mListener
}
并在您的活动中调用方法时编写以下代码
fragmentInstance.getListener().receivePreview();