简体   繁体   English

如何实现导航抽屉

[英]How to Implement Navigation Drawer

I've followed the android developer website building the navigation drawer but can not seem to find out how to implement separate activities/fragments for each section. 我已经跟随android开发人员网站构建导航抽屉,但似乎无法找出如何为每个部分实现单独的活动/片段。 The code I have so far is below: 我到目前为止的代码如下:

@Override
public void onNavigationDrawerItemSelected(int position) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager
            .beginTransaction()
            .replace(R.id.container,
                    PlaceholderFragment.newInstance(position + 1)).commit();
}

When implementing the action bar tab menu the code I use is below: 实施操作栏标签菜单时,我使用的代码如下:

@Override
    public Fragment getItem(int index) {
        switch (index) {
        case 0:
            return new FirstTabFragment();
        case 1:
            return new SecondTabFragment();
        case 2:
        }
        return null;
    }

I've tried about 20 different implementations of the 2nd example code to try and get it to work with the navigation drawer instead of actionbar tabs. 我尝试了第二个示例代码的大约20种不同实现,以使其与导航抽屉(而不是操作栏选项卡)一起使用。 Anyone who can point me in the right direction would be doing me a hige favour. 任何能将我指向正确方向的人都会帮我一个忙。

The entire MainActivity.java file is below just in case that is of any use. 整个MainActivity.java文件位于下面,以防万一。

public class MainActivity extends ActionBarActivity implements
    NavigationDrawerFragment.NavigationDrawerCallbacks {


private NavigationDrawerFragment mNavigationDrawerFragment;


private CharSequence mTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
            .findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager
            .beginTransaction()
            .replace(R.id.container,
                    PlaceholderFragment.newInstance(position + 1)).commit();
}

public void onSectionAttached(int number) {
    switch (number) {
    case 1:
        mTitle = getString(R.string.title_section1);
        break;
    case 2:
        mTitle = getString(R.string.title_section2);
        break;
    case 3:
        mTitle = getString(R.string.title_section3);
        break;
    case 4:
        mTitle = getString(R.string.title_section4);
        break;
    case 5:
        mTitle = getString(R.string.title_section5);
        break;
    case 6:
        mTitle = getString(R.string.title_section6);
        break;
    case 7:
        mTitle = getString(R.string.title_section7);
        break;
    case 8:
        mTitle = getString(R.string.title_section8);
        break;
    case 9:
        mTitle = getString(R.string.title_section9);
        break;
    case 10:
        mTitle = getString(R.string.title_section10);
        break;
    }
}

public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {

        getMenuInflater().inflate(R.menu.main, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    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);
        TextView textView = (TextView) rootView
                .findViewById(R.id.section_label);
        textView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
        return rootView;
    }

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

} }

Here are a good tutorial for you. 这是一个适合您的好教程。

http://www.androidhive.info/category/navigation-drawer/ http://www.androidhive.info/category/navigation-drawer/

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

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