简体   繁体   English

带选项卡的抽屉导航

[英]Drawer navigation with Tabs

Googling for three days. 谷歌搜索三天。 Can't help myself. 不能帮助自己。 I have navigation drawer and fragment and everything works perfect except tabs in fragment. 我有导航抽屉和片段,并且除片段中的选项卡外,其他所有东西都完美运行。 This is part of MainActiviti Class... 这是MainActiviti类的一部分...

case R.id.linearLayoutHome:
        fragment = new HomeFragment();
        break; //this one works well

case R.id.linearLayoutHome:
        fragment = new CategoryFragment(); //error showing here
        break; //this one doesn't work

Calling fragment... 正在呼叫片段...

FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();

Home fragment which works perfect: 完美的家庭片段:

public class HomeFragment extends Fragment {
public HomeFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    return rootView;
     }

}

CategoryFragment which doesn't work. CategoryFragment不起作用。 I think this is because of fragment in fragment: 我认为这是由于片段中的片段:

public class CategoriesActivity extends Fragment {

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

public CategoriesActivity(){
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());

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

    return rootView;
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

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

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

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

public static class DummySectionFragment extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tabbed_dummy, container, false);
        TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
        dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}

}

I need some solution. 我需要一些解决方案。 No more ideas. 没有更多的想法。

STEP 1: Your onCreateView() should look like this: 步骤1:您的onCreateView()应该如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    return rootView;
}

and add this method to your Fragment : 并将此方法添加到您的Fragment

@Override
public void onViewCreated(View view, Bundle savedInstanceState){
    mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
    mViewPager = (ViewPager) view.findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
}

STEP 2: Make sure that your Activity extends from FragmentActivity or ActionBarActivity . 步骤2:确保您的ActivityFragmentActivityActionBarActivity扩展。

STEP 3: Make sure that you are using android.support.v4.app.Fragment and not android.app.Fragment 步骤3:确保您使用的是android.support.v4.app.Fragment而不是android.app.Fragment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM