为什么不能使用switch语句在viewpager中的视图之间切换

时间:2014-08-01 18:39:17

标签: android android-fragments

我使用了这个turtorial:http://www.paulusworld.com/technical/android-navigationdrawer-sliding-tabs#comment-2793

在我的导航抽屉旁边获取滑动视图,但我无法弄清楚如何更改视图

在onCreateView中我试图用switch语句来做,但它说“方法不是从它的超类覆盖方法

片段代码:

public class StepFragment extends Fragment {

public static final String TAG = StepFragment.class.getSimpleName();
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;


public static StepFragment newInstance() {
    return new StepFragment();
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.step_fragment, container, false);
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getChildFragmentManager());

    mViewPager = (ViewPager) v.findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    return v;
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = new TabbedContentFragment();
        Bundle args = new Bundle();
        args.putInt(TabbedContentFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.step_title1).toUpperCase(l);
            case 1:
                return getString(R.string.step_title2).toUpperCase(l);
            case 2:
                return getString(R.string.step_title3).toUpperCase(l);
        }
        return null;
    }
}

public static class TabbedContentFragment extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    public TabbedContentFragment() {
    }

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

        View view1 = inflater.inflate(R.layout.fragment_step,
                container, false);

        View view2 = inflater.inflate(R.layout.fragment_step1,
                container, false);

        switch (position) {
            case 0:
                return view1;
            case 1:
                return view2;

        }
        return null;

    }
}

}

1 个答案:

答案 0 :(得分:2)

你的onCreateView传递了四个参数,超类方法只需要3个。你需要删除位置arg。

使用你在getItem()中设置的args Bundle来传递位置。在onCreateView()中,获取那些args并从该Bundle中拉出位置。

编辑:

这是你的getItem():

@Override
public Fragment getItem(int position) {
    Fragment fragment = new TabbedContentFragment();
    Bundle args = new Bundle();
    args.putInt(TabbedContentFragment.ARG_SECTION_NUMBER, position + 1);
    fragment.setArguments(args);
    return fragment;
}

由于onCreateView中的switch语句正在寻找0或1,所以继续从args.putInt()中删除“+ 1”:

@Override
public Fragment getItem(int position) {
    Fragment fragment = new TabbedContentFragment();
    Bundle args = new Bundle();
    args.putInt(TabbedContentFragment.ARG_SECTION_NUMBER, position);
    fragment.setArguments(args);
    return fragment;
}

现在,在您的片段中,您需要获取args并从中获取位置:

public static class TabbedContentFragment extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    public TabbedContentFragment() {
    }

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

        Bundle args = getArguments();
        int position = args.getInt(ARG_SECTION_NUMBER, 0);

        View view1 = inflater.inflate(R.layout.fragment_step,
                container, false);

        View view2 = inflater.inflate(R.layout.fragment_step1,
                container, false);

        switch (position) {
            case 0:
                return view1;
            case 1:
                return view2;

        }
        return null;

    }
}