0

我想扩展Fragment课程,但我希望能够将我的课程与 HoloEverywhere 和默认片段一起使用......

所以我尝试使用装饰器......

public abstract class RFragment<F extends Fragment, I extends LayoutInflater> extends Fragment implements IKey
{
    private final Class<F> mFragmentClass;
    private final Class<I> mInflatorClass;

    public RFragment(Class<F> fragmentClass, Class<I> inflatorClass)
    {
        this.mFragmentClass = fragmentClass;
        this.mInflatorClass = inflatorClass;
    }
}

我现在也想覆盖onCreateView,但我做不到。我尝试如下:

@Override
public View onCreateView(I inflater, ViewGroup container, Bundle savedInstanceState)
{
    if (view == null)
    {
        view = doOnCreateView(inflater, container, savedInstanceState);
        loadingIndicator = doGetLoadingIndicatorView(view);
    }
    return view;
}
protected abstract View doOnCreateView(I inflater, ViewGroup container, Bundle savedInstanceState);
protected abstract View doGetLoadingIndicatorView(View view);

但这是不允许的,因为如果参数不匹配,我无法覆盖该函数(I!= LayoutInflator)

有没有可能解决这个问题?我想避免复制我的类,只需将默认片段和默认 LayoutInflator 中的导入更改为 HoloEveryhwere 中的导入(仅扩展默认类)...

4

1 回答 1

0

您不能以这种方式覆盖该功能,因为 我建议您使用类似的东西I更准确:LayoutInflater

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    I infl = (I) inflater; // May be unchecked
    if (view == null)
    {
        view = doOnCreateView(infl, container, savedInstanceState);
        loadingIndicator = doGetLoadingIndicatorView(view);
    }
    return view;
}

事实上,我想知道为什么你需要为 I 使用 TypeVariable。你有 LayoutInflater 的子类?也许在任何地方都使用 LayoutInflater 就足够了?

于 2013-07-29T09:57:50.453 回答