java – 如何使用Android导航抽屉更改片段

我知道这些类型的问题已经在这里,但我还没有找到我的答案这个问题:

>我创建了一个应用程序,并使用已创建AUTOMATICLLY由应用程序(AndroidStudio)的导航抽屉

这里是我有:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
            .commit();
}

public void onSectionAttached(int number) {
    switch (number) {
        case 1:

            break;
        case 2:

            break;
        case 3:

            break;
    }
}

还有一些在这里:

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

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

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

我想使用导航抽屉中的按钮显示另一个片段。我想使用这个代码,所以请不要给我任何指南或教程制作自己的抽屉..

问题是,情况1:情况2:和情况3:如果我想打开另一个片段?
Thanx。

还有一个问题:

>如何添加更多片段和事务?
这不工作 –

Fragment fragment = new MyFragment1();
Fragment frag = new MyFragment2();
FragmentManager fragmentManager = getFragmentManager();
switch(position) {
    case 0:
        fragment = new MyFragment1();
        break;
    case 1:
        frag = new MyFragment2();

        break;
}
fragmentManager.beginTransaction()
        .replace(R.id.container, fragment).commit();

}}

你应该把一个switch语句放到onNavigationDrawerItemSelected方法中。

这样的东西应该工作:

public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    Fragment fragment;
    FragmentManager fragmentManager = getFragmentManager(); // For AppCompat use getSupportFragmentManager
    switch(position) {
        default:
        case 0:
            fragment = new MyFragment1();
            break;
        case 1:
            fragment = new MyFragment2();
            break;
    }
    fragmentManager.beginTransaction()
        .replace(R.id.container, fragment)
        .commit();
}

这只是快速完成,但我认为它应该工作

http://stackoverflow.com/questions/20638967/how-to-change-fragments-using-android-navigation-drawer

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:java – 如何使用Android导航抽屉更改片段