我有两个 Fragment
层次结构:
fragment
-> MyAbstractFragment1
-> MyAbstractFragment2
-> ... -> MyAbstractFragmentN
-> 我的 fragment
Fragment
-> ListFragment
-> MyAbstractListFragment1
-> MyAbstractListFragment2
-> ... -> MyAbstractListFragmentN
-> MyListFragment
MyAbstractFragmentN
和 MyAbstractListFragmentN
都实现了 IMyFragment
,因此 MyFragment
和 MyListFragment
也实现了这个接口(interface)。
有没有办法创建由实现 IMyFragment
的 Fragment
类型的字段?那么“MyListFragment”和“MyFragment”都是正确的类型吗?
我无法从 MyFragment
和 MyListFragment
继承创建父类(super class),因为它们从一开始就有很大不同。它们只有 Fragment
相同,以及来自 MyAbstractListFragmentN
和 MyAbstractFragmentN
的 IMyFragment
。
我想写:
class SomeClass{
private {FragmentImplementsIMyFragment} mFragment;
...
mFragment = new MyFragment();
...
mFragment = new MyListFragment();
}
我需要两种行为:来自 Fragment
和来自 IMyFragment
。
最佳答案
Is there any way to create field typed by Fragment which implements IMyFragment? So both 'MyListFragment' and 'MyFragment' would be correct type for it?
根据您的真正目标,您可能想要使用 IMyFragment
作为您的类型,或者在代码中进行一些检查以查看您的 Fragment 是否实现了所需的接口(interface)。您还可以检查创建的对象的类是什么并采取相应的操作
关于java - Android 类层次结构和与 Fragment 的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20907242/