简体   繁体   English

如何使用jfeinstein10 / SlidingMenu在Android中实现两级滑动菜单?

[英]How to implement two level slidingmenu in Android using jfeinstein10 / SlidingMenu?

I want to create 2 level sliding menu in android. 我想在android中创建2级滑动菜单。 When i click on the first sliding menu item i need to show another sliding menu on the left to it. 当我点击第一个滑动菜单项时,我需要在左侧显示另一个滑动菜单。 I created the first level sliding menu using the following code. 我使用以下代码创建了第一级滑动菜单。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView    
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/list_padding"
        android:paddingRight="@dimen/list_padding" />

</FrameLayout> 

Code Part 代码部分

SlidingMenu menu;
menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidth(10);
menu.setFadeDegree(0.0f);
menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
menu.setBehindWidth(200);
menu.setMenu(R.layout.menu_frame);

How to proceed to make the 2nd level sliding menu? 如何进行二级滑动菜单?

Try with this: 试试这个:

SlidingMenu menu;
menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT_RIGHT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidth(10);
menu.setFadeDegree(0.0f);
menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
menu.setBehindWidth(200);
menu.setMenu(R.layout.menu_frame);
//Set the secondary menu
menu.setSecondaryMenu(R.layout.menu_frame);
menu.setSecondaryShadowDrawable(R.drawable.shadow);

We can achieve the SlideMenu using the TranslateAnimation in android. 我们可以在android中使用TranslateAnimation实现SlideMenu。 Create a frameLayout in XML and have MainXML page and then MainSlidingMenu. 在XML中创建一个frameLayout并拥有MainXML页面,然后是MainSlidingMenu。 Adjacent to the MainSlidingMenu have the secondary slidingMenu. 邻近MainSlidingMenu具有辅助slidingMenu。 By default have the MainSlidingMenu and Secondary SlidingMenu invisible. 默认情况下,MainSlidingMenu和Secondary SlidingMenu不可见。 If the button click or any required event happend show the Main SlidingMenu , then if there is a click/event in the MainSlidingmenu translate the MainMenu further to show the secondary sliding menu. 如果按钮单击或任何所需事件发生显示Main SlidingMenu,则如果MainSlidingmenu中有单击/事件,则进一步翻译MainMenu以显示辅助滑动菜单。

This is how I got it, using the mentioned library: 这就是我如何使用提到的库:

This layout contains the top level sliding menu, which references the layout of the menu (fragment_nav_menu) and the layout with the sub-menu layout reference as view above. 此布局包含顶级滑动菜单,该菜单引用菜单的布局(fragment_nav_menu)和子菜单布局参考的布局,如上图所示。

<com.jeremyfeinstein.slidingmenu.lib.SlidingMenu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:sliding="http://schemas.android.com/apk/res-auto"
    android:id="@+id/slidingMenuRoot"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    sliding:viewAbove="@layout/sliding_sub_menu"
    sliding:viewBehind="@layout/fragment_nav_menu"
    sliding:touchModeAbove="fullscreen"
    />

This would be the second level menu (sliding_sub_menu.xml), note that what you set as viewAbove here is going to be the actual top-level content. 这将是第二级菜单(sliding_sub_menu.xml),请注意您在此处设置为viewAbove的内容将是实际的顶级内容。

<com.jeremyfeinstein.slidingmenu.lib.SlidingMenu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:sliding="http://schemas.android.com/apk/res-auto"
    android:id="@+id/slidingSubMenuRoot"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    sliding:viewAbove="@layout/fragment_content"
    sliding:viewBehind="@layout/fragment_sliding_menu"
    sliding:touchModeAbove="fullscreen"
/>

The content layout (fragment_content.xml) might be like this, a simple FrameLayout, and then, programmatically, you would add the desired fragment. 内容布局(fragment_content.xml)可能是这样的,一个简单的FrameLayout,然后以编程方式添加所需的片段。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainContentFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/app_background"
    />

Likewise, the sliding sub-menu content, is defined in the layout file (fragment_sliding_menu.xml) and used programmatically to ad a Fragment instance. 同样,滑动子菜单内容在布局文件(fragment_sliding_menu.xml)中定义,并以编程方式用于广告Fragment实例。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainContentFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/app_background"
    />

To add a fragment to those FrameLayouts, use something like this (maybe also removing a potential previous fragment before adding a new one): 要向这些FrameLayouts添加片段,请使用类似的内容(也可以在添加新片段之前删除潜在的先前片段):

    FragmentTransaction fragTrans = getSupportFragmentManager().beginTransaction();
    fragTrans.add(R.id.slidingSubMenuFrame, SubMenuFragment.newInstance(this));
    fragTrans.commit();

I haven't tested it much yet, but seems to work. 我还没有测试它,但似乎工作。 Of course, further logic is needed to implement the desired behavior of the menus (close listener, select items, etc). 当然,需要进一步的逻辑来实现菜单的期望行为(关闭监听器,选择项目等)。

JFeinstein sliding menu is a rich library. JFeinstein滑动菜单是一个丰富的库。 You can easily add as many menu levels as you desired. 您可以根据需要轻松添加任意数量的菜单级别。 The idea is to use sliding menu as left or right sliding view of main sliding menu and so on. 这个想法是使用滑动菜单作为主滑动菜单的左或右滑动视图等。 Here is a stand alone example which provides 2 level menu. 这是一个独立的例子,提供2级菜单。 What you need is to import JFeinstein sliding menu as library and extend your activity from SlidingFragmentActivity. 您需要的是将JFeinstein滑动菜单导入为库并从SlidingFragmentActivity扩展您的活动。 I am putting complete activity code with comments to make things more clear. 我将完整的活动代码与评论相结合,以使事情更加清晰。

 public class MainActivity extends SlidingFragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // dummy views - content view
        TextView content = new TextView(this);
        content.setBackgroundColor(Color.WHITE);
        content.setText("content");
       // Menu view
        TextView menu = new TextView(this);
        menu.setBackgroundColor(Color.GREEN);
        menu.setText("menu");
        // 2nd level menu view
        TextView subMenu = new TextView(this);
        subMenu.setBackgroundColor(Color.LTGRAY);
        subMenu.setText("submenu");


        //configure sliding menu
        SlidingMenu sm = getSlidingMenu();
        sm.setMode(SlidingMenu.SLIDING_WINDOW);
        sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        sm.setBehindOffset(80);
        sm.setBehindScrollScale(0.25f);
        sm.setFadeDegree(0.25f);

        //Another sliding menu - for 2nd level or sub menu 
        SlidingMenu leftSlidingView = new SlidingMenu(this);
        leftSlidingView.setMode(SlidingMenu.SLIDING_WINDOW);
        leftSlidingView.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        leftSlidingView.setBehindOffset(80);
        leftSlidingView.setBehindScrollScale(0.25f);
        leftSlidingView.setFadeDegree(0.25f);

    //==== Required instruments has been created ;) lets put them at right places   

        // setting menu and sub-menu view 
        leftSlidingView.setContent(menu);  // at center of left sliding view
        leftSlidingView.setMenu(subMenu);  // at left of left sliding view

        //set content view
        setContentView(content);           // at center of main sliding view
        // finally, set  leftSlidingView as behind content  view of main view
        setBehindContentView(leftSlidingView); // at left of main sliding view

    }
}

Here is the result: 结果如下:

在此输入图像描述

Hope it would help :) 希望它会有所帮助:)

I figured it out, it's so simple. 我想通了,这很简单。

menu.setMenu(R.layout.layout_filemenu);
menu.setSecondaryMenu(R.layout.layout_main);
menu.setMode(SlidingMenu.LEFT_RIGHT);

setMenu -> Left side SetSecondaryMenu ->Right side setMenu - >左侧SetSecondaryMenu - >右侧

Hope this helps 希望这可以帮助

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

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