导航抽屉和右侧菜单之间的冲突

时间:2014-12-15 10:28:20

标签: android android-activity navigation-drawer slidingmenu android-sliding

我需要你的帮助。我在android编程中迈出了第一步!

SOS !!!

我创建了经典的导航抽屉,您可以通过单击汉堡图标或从左侧滑动来打开/关闭。我还创建了右侧菜单(如在Facebook应用程序中)并使用了GitHub(https://github.com/jfeinstein10/SlidingMenu)中的这个库。最后,我在抽屉导航菜单和右侧菜单之间存在冲突

当导航抽屉菜单打开并且您想通过从右向左滑动打开右侧菜单来关闭它。

所以我的问题:如何说设备当导航抽屉菜单打开时你不能通过滑动打开右侧菜单,通过滑动你可以关闭导航抽屉。

我在MainActivity中的右侧菜单代码

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.RIGHT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
menu.setMenu(R.layout.sidemenu);
menu.setBehindWidthRes(R.dimen.slidingmenu_behind_width);

String[] items = {"Fisrt fragment","Second fragment"};
((ListView) findViewById(R.id.sidemenu)).setAdapter(
        new ArrayAdapter<Object>(
                this,
                R.layout.sidemenu_item,
                R.id.text,
                items
        )
);

NavigationDrawer代码:

public class NavigationDrawerFragment extends Fragment {
public static final String PREF_FILE_NAME="testpref";
public static final String KEY_USER_LEARNED_DRAWER="user_learned_drawer";

private ActionBarDrawerToggle mDrawerToggle;
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;

private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;

private View containerView;

public NavigationDrawerFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mUserLearnedDrawer = Boolean.valueOf(readFromPreferences(getActivity(),KEY_USER_LEARNED_DRAWER,"false"));
    if(savedInstanceState!=null){
        mFromSavedInstanceState=true;
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
}

public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar){
    containerView = getActivity().findViewById(fragmentId);
    mDrawerLayout = drawerLayout;
    mDrawerToggle = new ActionBarDrawerToggle(getActivity(),drawerLayout,toolbar,R.string.drawer_open, R.string.drawer_close){

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if(!mUserLearnedDrawer){
                mUserLearnedDrawer=true;
                saveToPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, mUserLearnedDrawer+"");
            }
            getActivity().invalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            getActivity().invalidateOptionsMenu();
        }

        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);

            if(slideOffset<0.6) {
                toolbar.setAlpha(1 - slideOffset);
            }
        }
    };
    if(!mUserLearnedDrawer&&!mFromSavedInstanceState){
        mDrawerLayout.openDrawer(containerView);
    }
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
    });

}
public static void saveToPreferences(Context context,String preferenceName, String preferenceValue){
    SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(preferenceName, preferenceValue);
    editor.apply();
}
public static String readFromPreferences(Context context, String preferenceName, String defaultValue){
    SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_APPEND);
    return  sharedPreferences.getString(preferenceName, defaultValue);
}

2 个答案:

答案 0 :(得分:0)

添加其他级别,例如:http://pastebin.com/xm8UZbvj

答案 1 :(得分:0)

当用户尝试打开右侧菜单,然后检查导航抽屉是否打开,然后使用以下代码关闭它 -

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