简体   繁体   English

在一个活动中组织多个Fragment接口

[英]Organize multiple Fragment interfaces in an Activity

I'm creating an app that uses multiple fragments in a single activity, and most of the fragments need to interact with the Activity. 我正在创建一个在单个活动中使用多个片段的应用程序,并且大多数片段都需要与活动进行交互。 I'm using a separate Interface for each fragment, which the Activity implements. 我为Activity实现的每个片段使用单独的接口。

To give you an idea of how it looks like presently 让您了解目前的状况

public class MainActivity extends AppCompatActivity implements
    Fragment1.Fragment1Interface,
    Fragment2.Fragment2Interface,
    ListAdapter1.ListAdapter1Interface,   
    Fragment3.Fragment3Interface,
    MyDrawerLayout.MyDrawerCallbacks {

       /* Code omitted for brevity... */

        @Override
        public void onCancel(long id) {

        }

        @Override
        public void onSave(long id) {

        }

}

As you can see, there are quite a lot of interfaces that need to be implemented. 如您所见,有很多接口需要实现。 This is okay, but what bothers me is that the implementation of the methods of the various interfaces don't give any clue about which interface/fragment they belong to. 没关系,但是让我感到困扰的是,各种接口方法的实现并没有提供关于它们属于哪个接口/片段的任何线索。 onCancel() and onSave() are very generic, so by only looking at the code there is no way to know if they belong to Fragment1Interface , Fragment2Interface , etc. onSave() onCancel()onSave()非常通用,因此仅查看代码就无法知道它们是否属于Fragment1InterfaceFragment2Interface等。

Of course, I could prefix each interface method with the interface name, resulting in something like 当然,我可以在每个接口方法的前面加上接口名称,结果是

@Override
public void Fragment1Interface_onCancel(long id) {

}

@Override
public void Fragment2Interface_onSave(long id) {

}

and so on. 等等。

Is there any easy way to achieve some kind of "scoping" in regards to interface implementations? 是否有任何简单的方法可以实现有关接口实现的某种“作用域”? I know I could pass an object to each fragment which implements the respective interface, but it's quite a bit more work than simply relying on the Activity which can be easily retrieved by getActivity() from within the Fragment. 我知道我可以将一个对象传递给实现各自接口的每个片段,但是这比仅依赖Activity可以完成很多工作,而Activity可以通过Fragment中的getActivity()轻松检索。

Another option I considered was to have a common interface like 我考虑的另一种选择是拥有一个通用的界面,例如

public interface FragmentInteractionInterface{
    public FragmentInterface getInterface(Fragment fragment);
}

public interface FragmentInterface{}


public interface Fragment1Interface extends FragmentInterface{
    public void onCancel(long id);
}

public interface Fragment2Interface extends FragmentInterface{
    public void onSave(long id);
}

The idea being here that the Activity would only implement the FragmentInteractionInterface, and return a specific implementation of FragmentInterface based on the type of fragment in getInterface() , like so 这里的想法是,Activity将仅实现FragmentInteractionInterface,并根据getInterface()的片段类型返回FragmentInterface的特定实现,如下所示

public FragmentInterface getInterface(Fragment fragment){
    if( fragment instanceof Fragment1 )
        return mFragment1InterfaceImplementation;
    else if( fragment instanceof Fragment2 )
        return mFragment2InterfaceImplementation;
}


Fragment1Interface mFragment1InterfaceImplementation = new Fragment1Interface(){
    @Override
    public void onCancel(long id){
        // Do something
    }
}

Fragment2Interface mFragment2InterfaceImplementation = new Fragment2Interface(){
    @Override
    public void onSave(long id){
        // Do something
    }
}

(Obviously the names of the fragments are more descriptive in real life!) (显然,片段的名称在现实生活中更具描述性!)

The only other (easy) way I can think of is to surround each of the implemented interface methods with comments, grouping them together (some interfaces contain many methods). 我能想到的唯一(简便)的方法是用注释将每个已实现的接口方法包围起来,将它们组合在一起(某些接口包含许多方法)。

It seems like there should be a better way of achieving this kind of "grouping" in an easy way, without resorting to having to pass separate implementation objects to each fragment... any ideas? 似乎应该有一种更好的方法,以一种简单的方式实现这种“分组”,而不必诉诸于将单独的实现对象传递给每个片段……有什么想法吗?

In general, it's a good practice to prevent clients (fragments) knowing about methods they don't use. 通常,防止客户(片段)了解他们不使用的方法是一个好习惯。 Such interfaces called role interfaces, so I'd stick with interface per fragment approach. 这种接口称为角色接口,因此我会坚持使用每个片段的接口方法。

Answering your question, why you're so convinced that implementer should explicitly specify interface/method relation? 回答您的问题,为什么您如此确信实现者应明确指定接口/方法关系? You pointed that method names are too generic, but that's naming problem. 您指出方法名称太通用,但这是命名问题。 Actions scoped to fragment should stay there, and actions logically related to activity, therefore, would have the appropriate naming. 范围限定为片段的动作应保留在此处,并且与活动在逻辑上相关的动作应具有适当的命名。

If you still persist on this idea of clean separation, I'd recommend using some sort of annotation processing. 如果您仍然坚持干净分离的想法,建议您使用某种注释处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM