2013-02-27 55 views
8

我有Android的註釋和繼承的麻煩:Android的註解和繼承

@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(); 
    } 
    ... 

我怎麼能確保afterTestBaseFragmentViews()被afterTestFragmentViews之前稱爲() ? 或者你能指出我的任何文檔描述如何使用AndroidAnnotations繼承?

+0

https://github.com/androidannotations/androidannotations/issues/810#issuecomment-31218529 – 2016-11-07 21:43:03

回答

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 
    } 

} 

我希望這是你所期待的。 (未測試 - 只是寫在記事本)

+4

方法'''afterTestBaseFragmentViews'''不應該是私人的。在這種情況下,AA無法訪問此方法 – ruX 2013-06-05 17:39:48

+0

如果TestBaseFragment具有像ViewById這樣的註釋,該怎麼辦?在我的情況下,在生成的類TestFragment_中看不到這些註釋。註釋相同單擊。 – 2016-03-08 10:41:52