8

我在使用 android-annotations 和继承时遇到了问题:

@EFragment(R.layout.fragment_foo)
public class TestBaseFragment {
    @AfterViews
    public void afterTestBaseFragmentViews() {
   }
}

@EFragment(R.layout.fragment_foo)
public class TestFragment extends TestBaseFragment {
    @AfterViews
    public void afterTestFragmentViews() {
   }
}

生成:

public final class TestFragment_
    extends TestFragment
{
    ...

    private void afterSetContentView_() {
        afterTestFragmentViews();
        afterTestBaseFragmentViews();
    }
    ...

如何确保在 afterTestFragmentViews() 之前调用 afterTestBaseFragmentViews() ?或者你能给我指出任何描述如何使用 AndroidAnnotations 进行继承的文档吗?

4

1 回答 1

11

这只是某种解决方法

使用抽象类:

@EFragment(R.layout.fragment_foo)
public abstract class TestBaseFragment {

    @AfterViews
    protected void afterTestBaseFragmentViews() {
        // do something
        afterView();
    }

    protected abstract void afterView();

}

@EFragment(R.layout.fragment_foo)
public class TestFragment extends TestBaseFragment {

    @Override
    protected void afterView() {
        // do something after BaseFragment did something
    }

}

使用简单的子类化:

@EFragment(R.layout.fragment_foo)
public class TestBaseFragment {

    @AfterViews
    protected void afterTestBaseFragmentViews() {
        afterView();
    }

    public void afterView() {
        // do something
    }

}

@EFragment(R.layout.fragment_foo)
public class TestFragment extends TestBaseFragment {

    @Override
    public void afterView() { 
        super.afterView();
        // do something after BaseFragment did something
    }

}

我希望这就是你要找的。(未测试 - 只是用记事本写的)

于 2013-02-27T18:32:31.913 回答