且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android 选项卡式活动:带有 ViewPager 的操作栏选项卡:每个选项卡的布局不同

更新时间:2022-05-11 07:44:04

你需要为每个页面制作不同的类.页面从片段延伸.您可以为每个片段加载不同的 layout-xml.

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.

您可以删除整个 placeholderfragment 类.它不再需要了.

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