如何使用MenuDrawer显示第一个项目的视图

时间:2014-05-03 17:40:00

标签: android menu navigation-drawer

我正在使用这个库

  

http://simonvt.github.io/android-menudrawer/

我想要做的是,显示启动应用程序后的第一个片段。现在,当我启动应用程序时,视图只是白色(由r.layout.main定义)但我需要显示我的第一个片段= Prehlad

这是我的代码,不包括适配器。

private MenuDrawer mMenuDrawer;

        private MenuAdapter mAdapter;
        private ListView mList;

        private int displayView = -1;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);


        /*    ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
            // You can also assign the title programmatically by passing a
            // CharSequence or resource id.
           // actionBar.setTitle(R.string.mList);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAction(new Drawer ());*/



            mMenuDrawer = MenuDrawer.attach(this, MenuDrawer.MENU_DRAG_CONTENT);
            mMenuDrawer.setContentView(R.layout.main);
            mMenuDrawer.setTouchMode(MenuDrawer.TOUCH_MODE_FULLSCREEN);

            List<Object> items = new ArrayList<Object>();
            items.add(new Category("SOŠ Polytechnická"));
            items.add(new Item("Prehľad", R.drawable.home));
            items.add(new Item("Novinky", R.drawable.http));   
            items.add(new Item("Rozvrh hodín", R.drawable.schedule));
            items.add(new Item("Zastupovanie", R.drawable.zastupovanie));
            items.add(new Item("Jedálny lístok", R.drawable.food));
            items.add(new Item("Informácie o odboroch", R.drawable.odbory));
            items.add(new Item("Zvonenia", R.drawable.bell));
            items.add(new Item("Stiahnuteľné súbory", R.drawable.download));
            items.add(new Category("L&K Developers"));
            items.add(new Item("O nás", R.drawable.iconlnk));
            items.add(new Item("O aplikácií", R.drawable.info));
        /*    items.add(new Item("Item 9", R.drawable.ic_action_refresh_dark));
            items.add(new Item("Item 10", R.drawable.ic_action_select_all_dark));*/

            mList = new ListView(this);
            mAdapter = new MenuAdapter(items);
            mList.setAdapter(mAdapter);
            mList.setOnItemClickListener(mItemClickListener);

            mMenuDrawer.setMenuView(mList);




         /*   mContentTextView = (TextView) findViewById(R.id.contentText);
            mContentTextView.setText(mContentText);*/

            mMenuDrawer.setOnInterceptMoveEventListener(new MenuDrawer.OnInterceptMoveEventListener() {
                public boolean isViewDraggable(View v, int dx, int x, int y) {
                    return v instanceof SeekBar;

                }

            });

        }



        private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                 displayView(position);
                 mMenuDrawer.setActiveView(view, position);

            }


                /**
                 * Diplaying fragment view for selected nav drawer list item
                 * */
                private void displayView(int position) {
                    // update the main content by replacing fragments
                    android.app.Fragment fragment = null;
                    switch (position) {
                    case 1:
                        fragment = new Prehlad();
                        break;
                    case 2:
                        fragment = new Novinky();
                        break;
                    case 3:
                        Calendar calender = Calendar.getInstance();
                        int tyzdenid = 0;
                        CharSequence tyzden="neparny";
                        if(calender.get(Calendar.WEEK_OF_YEAR)%2==0){tyzdenid=1; tyzden="parny";}
                        Intent r = new Intent(MainActivity.this, Rozvrh.class);
                        Bundle optionRozvrhy = new Bundle();
                        optionRozvrhy.putInt("optionZU",0); //Your id
                        optionRozvrhy.putInt("optionNP",tyzdenid);
                        optionRozvrhy.putBoolean("c", false);
                        optionRozvrhy.putCharSequence("optionZUS","ziacke");
                        optionRozvrhy.putCharSequence("optionNPS",tyzden);
                        r.putExtras(optionRozvrhy); 
                        startActivity(r);
                        break;

                    case 4:
                        fragment = new Zastupovanie();
                        break;
                    case 5:
                         Intent ob = new Intent(MainActivity.this, ObedyJPGActivity.class);
                        startActivity(ob);
                        break;
                    case 6:
                        fragment = new Odbory();
                        break;
                    case 7:
                        fragment = new Zvonenia();
                        break;
                    case 8:
                        fragment = new UcebnyMaterial();
                        break;
                    case 9:
                        fragment = new AboutUs();
                        break;
              /*      case 10:
                        fragment = new About();
                        break;*/

                    default:
                        break;
                    }

                    if (fragment != null) {
                        android.app.FragmentManager fragmentManager = getFragmentManager();
                        fragmentManager.beginTransaction()
                                .replace(R.id.frame_container, fragment).commit();

                        // update selected item and title, then close the drawer
                        mList.setItemChecked(position, true);
                        mList.setSelection(position);
                      //  setTitle(navMenuTitles[position]);
                        mMenuDrawer.closeMenu();
                    } else {
                        // error in creating fragment
                        Log.e("MainActivity", "Error in creating fragment");
                    }

                }


            };


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    mMenuDrawer.toggleMenu();
                    return true;
            }

            return super.onOptionsItemSelected(item);
        }


        @Override
        public void onBackPressed() {
            final int drawerState = mMenuDrawer.getDrawerState();
            if (drawerState == MenuDrawer.STATE_OPEN || drawerState == MenuDrawer.STATE_OPENING) {
                mMenuDrawer.closeMenu();
                return;
            }

            super.onBackPressed();
        }

        private static class Item {

            String mTitle;
            int mIconRes;

            Item(String title, int iconRes) {
                mTitle = title;
                mIconRes = iconRes;
            }
        }

        private static class Category {

            String mTitle;

            Category(String title) {
                mTitle = title;
            }
        }

有人可以帮我解决这个问题吗?我真的很感激每一个帮助。

1 个答案:

答案 0 :(得分:0)

在onCreate中调用此方法         addHomeFragment();

这里是实现

private void addHomeFragment(){         SherlockFragment片段=新的HomeFragment();         if(fragmentManager == null)             fragmentManager = getSupportFragmentManager();         fragmentManager.beginTransaction()                 .add(R.id.container_frameLayout,fragment).commit();     }

并且片段的其余部分基于位置加载到clickevent上 希望这会有所帮助。