2

If I want to create a tabbed activity with more than 10 swipable fragments, what other option do I have rather than to create 10 different Fragment classes with layouts and inflate they based on their position in the tabbed layout.

That obviously sounds like a lot of duplicated code.

My question, fair and simple, is there any other way to avoid this?

4

1 回答 1

0

您可以创建一个片段类,然后int根据其在选项卡中的位置使用该值来确定要在onCreate通常为单个布局视图膨胀的方法中膨胀哪个布局。

例子

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(tabNumber == 1){
        setContentView(R.layout.fragment_layout1);
    }else if(tabNumber == 2){
        setContentView(R.layout.fragment_layout2);
    }else{
        setContentView(R.layout.fragment_layout3);
    }

}
于 2017-02-25T20:17:07.697 回答