菜单 - >子菜单 - >幻灯片抽屉菜单中的子菜单是否可以在android中?

时间:2015-04-16 06:13:39

标签: android android-drawable

我正在尝试在Android中使用这种格式Menu->submenu->submenu创建一个幻灯片抽屉菜单,但它不起作用,如果可能的话,我还需要如何设置最终子菜单onclick事件。

请帮助我,我需要这个,并在一个月内冲浪,但仍然没有得出任何结论。

请给我一些关于这个概念的想法

代码:

    public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    //private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    public static int cart;
    static boolean read=true;
    InputStream is=null;
    String result=null;
    String line=null;
    String mail_id;
    int code;
    static boolean changebackground=true;
    //expandable listview
    ExpandableListAdapter listAdapter;
    ExpandableListView mDrawerList;;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;
    //expandable listview
    TextView totalitems;
    DatabaseHandler data;
    ImageView gotocart;
    List<String> OurTemplates;
    // nav drawer title
    private CharSequence mDrawerTitle;

    // used to store app title
    private CharSequence mTitle;

    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        changebackground=false;




        final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
                R.layout.actionbar,
                null);

 ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
                    ActionBar.LayoutParams.MATCH_PARENT,
                    ActionBar.LayoutParams.MATCH_PARENT);
        // Set up your ActionBar
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(actionBarLayout);

actionBarLayout.setLayoutParams(lp);

       Button menu=(Button) actionBarLayout.findViewById(R.id.menu);

     totalitems=(TextView) actionBarLayout.findViewById(R.id.detail_totalcartitem);
        data=new DatabaseHandler(getApplicationContext());



        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    //  mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
        mDrawerList = (ExpandableListView) findViewById(R.id.list_slidermenu);
        mDrawerList.setGroupIndicator(null);
     prepareListData();
        navDrawerItems = new ArrayList<NavDrawerItem>();

        // adding nav drawer items to array
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1), true, ""));
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));

        // Recycle the typed array
        navMenuIcons.recycle();



        //expandable listview






        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        mDrawerList.setAdapter(listAdapter);

        // Listview Group click listener
        mDrawerList.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                // Toast.makeText(getApplicationContext(),
                // "Group Clicked " + listDataHeader.get(groupPosition),
                // Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        // Listview Group expanded listener
        mDrawerList.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                /*Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Expanded",
                        Toast.LENGTH_SHORT).show();*/
            }
        });

        // Listview Group collasped listener
        mDrawerList.setOnGroupCollapseListener(new OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
            /*  Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Collapsed",
                        Toast.LENGTH_SHORT).show();*/

            }
        });

        // Listview on child click listener
        mDrawerList.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                /
                int pos=childPosition;
                listDataHeader = new ArrayList<String>();
                listDataChild = new HashMap<String, List<String>>();
                changebackground=true;

                if(pos==0)
                {
                // Adding child data
                listDataHeader.add("E-commerce Templates");


                // Adding child data
                List<String> E_commerce = new ArrayList<String>();
                E_commerce.add("Cs-Cart");
                E_commerce.add("Magento");
                E_commerce.add("Opencart");
                E_commerce.add("Prestashop");
                E_commerce.add("Abantecart");
                listDataChild.put(listDataHeader.get(0), E_commerce); 

                listAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader, listDataChild);

                // setting list adapter
                mDrawerList.setAdapter(listAdapter);
                mDrawerList.expandGroup(0, true);
                }
                else if(pos==1)
                {
                    listDataHeader.add("CMS Templates");
                    List<String> CMS = new ArrayList<String>();
                    CMS.add("Wordpress");
                    CMS.add("Drupal");
                    listDataChild.put(listDataHeader.get(0), CMS); 
                    listAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader, listDataChild);
                    mDrawerList.setAdapter(listAdapter);
                    mDrawerList.expandGroup(0, true);
                }
                else if(pos==2)
                {
                    listDataHeader.add("Video Sharing Templates");
                    List<String> video_sharing = new ArrayList<String>();
                    video_sharing.add("Clipshare");
                    video_sharing.add("Phpmotion");
                    listDataChild.put(listDataHeader.get(0), video_sharing); 
                    listAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader, listDataChild);
                    mDrawerList.setAdapter(listAdapter);
                    mDrawerList.expandGroup(0, true);
                }
                else if(pos==3)
                {
                    listDataHeader.add("Social Networking Templates");
                    List<String> social_networking = new ArrayList<String>();
                    social_networking.add("Dolphin");
                    social_networking.add("Socialengine");
                    listDataChild.put(listDataHeader.get(0), social_networking); 
                    listAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader, listDataChild);
                    mDrawerList.setAdapter(listAdapter);
                    mDrawerList.expandGroup(0, true);
                }
                else if(pos==4)
                {
                    listDataHeader.add("Free Templates");
                    List<String> free_templates = new ArrayList<String>();
                    free_templates.add("Cs-Cart");
                    free_templates.add("Magento");
                    free_templates.add("Opencart");
                    free_templates.add("Prestashop");
                    free_templates.add("Dolphine");
                    free_templates.add("Drupal");
                    listDataChild.put(listDataHeader.get(0), free_templates); 
                    listAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader, listDataChild);
                    mDrawerList.setAdapter(listAdapter);
                    mDrawerList.expandGroup(0, true);
                }


                return false;
            }
        });





        ;

menu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!mDrawerLayout.isDrawerOpen(mDrawerList)) {
                    mDrawerLayout.openDrawer(mDrawerList);

                }
                else if (mDrawerLayout.isDrawerOpen(mDrawerList)){
                    mDrawerLayout.closeDrawer(mDrawerList);


                }

            }
        });

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer,
                R.string.app_name,
                R.string.app_name 
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle("E-commerce Templates");
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            displayView(0);
        }




    }


    private class SlideMenuClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            displayView(position);
        }
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        return super.onPrepareOptionsMenu(menu);
    }

    private void displayView(int position) {

        switch (position) {
        case 0:
            //fragment = new Home();
            // click();

            ///mDrawerLayout.closeDrawer(mDrawerList);
            break;
        case 1:
            Intent i=new Intent(MainActivity.this, CsCart.class);
            startActivity(i);
            break;
        case 2:
             click();
            break;
        case 3:
             click();
            break;
        case 4:
             click();
            break;
        case 5:
             click();
            break;

        default:
            break;
        }


    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }


    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }



    void click()
    {

        navDrawerItems.clear();


    }


    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add("Home");
        listDataHeader.add("Our Templates");
        listDataHeader.add("Our Services");
        listDataHeader.add("Portfolio");
        listDataHeader.add("Add-ons");
        listDataHeader.add("Blog");
        listDataHeader.add("Forum");
        listDataHeader.add("Contact us");


        // Adding child data
        OurTemplates = new ArrayList<String>();
        OurTemplates.add("E-commerce Templates");
        OurTemplates.add("CMS Templates");
        OurTemplates.add("Video Sharing Templates");
        OurTemplates.add("Social Networking Templates");
        OurTemplates.add("Free Templates");

        listDataChild.put(listDataHeader.get(0),new ArrayList<String>() );
        listDataChild.put(listDataHeader.get(1), OurTemplates);
        listDataChild.put(listDataHeader.get(2), new ArrayList<String>());
        listDataChild.put(listDataHeader.get(3), new ArrayList<String>());
        listDataChild.put(listDataHeader.get(4), new ArrayList<String>());
        listDataChild.put(listDataHeader.get(5), new ArrayList<String>());
        listDataChild.put(listDataHeader.get(6), new ArrayList<String>());
        listDataChild.put(listDataHeader.get(7), new ArrayList<String>());// Header, Child data

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

    }

}

1 个答案:

答案 0 :(得分:1)

我的个人经历

当我第一次学习Android时,我认为它很棒,因为它非常简单。您可以在线找到几乎任何问题的答案。

现在,我感觉不一样。因为我们在我的工作场所在Android中编写了一个复杂的工程工具,所以我们必须进行大量的自定义UI编程。这不好玩。这是一层地狱。 Android是一个痛苦的做自定义编码。

现在,这就是说。我会主张你坚持使用current Android standard。之后,你有一些选择。以下是我们所做的两个例子:

  1. &#39;关于&#39; - 在页面的顶部,我们有一个带有子菜单项的微调器的小片段。这些包括&#39;关于我们&#39; &#39;更新日志&#39; &#39;隐私政策&#39;等
  2. &#39;帮助&#39; - 整个页面都填充了所有子菜单项的Listview(每个帮助主题一个)。当您点击某个主题时,它会打开一个新片段,显示“之前的”字样。和&#39; next&#39;周围主题的按钮。
  3. 探索您的选择。如果您一直在寻找这个解决方案的数月,那么可能是时候灵活应对您的愿望了。

    找到更好的方法来整理您的信息

    请记住(取决于您的实施方式),导航复杂的菜单可能会很复杂且令人迷茫。尝试通过尽可能少的级别使用户尽可能轻松。查看显示different ways to organize complex navigation on a web page的网站。您可能会在移动设备上获得一些想法。 (提示:Listviews允许您将不可点击的标题作为项目...)以下是具有one sub-menu的应用的示例。做一些更多的研究,以便您确切地知道您想要实施什么以及如何实施。然后...

    编写自定义控件

    Android做得对的一件事是他们确实提供了有用的文档:Creating Custom Views。您可以扩展DrawerLayout控件以处理多个列表视图,或基本上您想要的任何内容。

    祝你好运!