java - 如何在java中的多个类中使用一个接口(interface)?

标签 java android android-fragments android-activity interface

I have one activity and 2 fragment.

I want when in activity fire listener.receivePreview(obj) then

  1. execute : OneFragment -> receivePreview .
  2. execute : 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) {

    }
}

最佳答案

我认为您可以使用观察者模式,这在您的情况下是一种很好的做法。

As described by GoF:

"Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically".

阅读更多信息 http://www.java2blog.com/2013/02/observer-design-pattern-in-java.html#TLio7G2ruqxvfZUR.99

在你的情况下,你有这样的关系(一对多),当 Activity 中发生事件时你想知道这两个 fragment 。

enter image description here

fragment 是实现观察者类,您的 Activity 具有主体角色,如上图所示。

我希望这可以帮助您以非常好的方式实现您的代码。 一些教程可以在以下链接中找到:

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

fragment 在这个设计模式中是正确定义的,所以我不改变他们的代码:

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) {

}

您需要引用 Activity 中的 fragment (作为观察者)。

ArrayList< OnReceiveListener > observers =  new ArrayList< OnReceiveListener>();

事实上,观察者可以订阅或注册自己到一个主题( fragment 持有对 Activity 的引用(最好使用单例模式!: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)
}
/* To avoid memory leaks, remember to unregister receivers when no longer observing */
public void unregisterObserver(OnReceiveListener observer) {
    observers.remove(observer);
}
notifyObservers(){
// call this method in the listener you want 

for( Observer obser : observers)
    obser. receivePreview(param )

}
...

//in fragment initialization: 
MainAct.getInstance().registerObserver(this)

关于java - 如何在java中的多个类中使用一个接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37482710/

相关文章:

android - PreferenceFragment 背景颜色

java - 如何从android中的值中选择数据

Android 在多段线的开始和结束处放置标记

java - 在 Windows 上更新桌面应用程序的 JRE 有哪些风险

java - 如何使用 Spring Security 重新加载用户更新权限

资源包中的 java.util.MissingResourceException

android - 在 Fragment 中使用 onPrepareOptionsMenu 而不是 onCreateOptionsMenu

java - 欺骗 postMethod (java 中的 apache)?

android - 在 Fragment 更改时更改状态栏颜色 [Android Lollipop]

java - Android:在父 fragment 和子 fragment 之间传递更新数据