Android Tabbed Activity: Action Bar Tabs with ViewPager: different layout for each tab

情到浓时终转凉″ 提交于 2019-12-01 01:12:51

You need to make different classes for each page. A page extends from a Fragment. You can just load a different layout-xml for every fragment.

public class FirstFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {


    View rootView = inflater.inflate(R.layout.<yourxmlhere>, container,
            false);
    return rootView;
}

}

You know have the fragment, but your adapter still needs to know which page (position) is which fragment. This is decided in the following function:

@Override
public Fragment getItem(int position) {
    switch (position){
    case 0:
    //page 1
    return new FirstFragment();
    break;

    case 1:
    //page 2
    return new SecondFragment();
    break;
    default:
    //this page does not exists
    return null;
}

Make sure you have set the correct amount of pages!

@Override
public int getCount() {
    //the amount of pages your adapter knows
    return <youramountofpages>;
}

This should get you up and running.

EDIT: You can just delete the whole placeholderfragment class. It is not needed anymore.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!