如何在android中实现滑动菜单

时间:2013-04-21 15:22:18

标签: android slider actionbarsherlock

我有滑动菜单库here&我想要左右滑动菜单来基于操作,在我的情况下,无论图标点击,总是从左到右滑动。这是代码

问题出在哪里?如何使两张幻灯片都能正常工作?

package com.example.simple_left_right_slider;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.actionbarsherlock.app.SherlockFragmentActivity;

import com.slidingmenu.lib.SlidingMenu;


public class MainActivity extends  FragmentActivity{

    private SlidingMenu menu;

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

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        menu = new SlidingMenu(this);
        menu.setTouchModeAbove(SlidingMenu.LEFT_RIGHT);
        menu.setMode(SlidingMenu.LEFT_RIGHT);
        menu.setShadowWidthRes(R.dimen.shadow_width);
        //menu.setShadowDrawable(R.drawable.shadow);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.menu_frame);
        getSupportFragmentManager()
          .beginTransaction()
          .replace(R.id.menu_frame, new SampleListFragment())
          .commit();

        menu.setSecondaryMenu(R.layout.menu_frame1);

        getSupportFragmentManager()
          .beginTransaction()
          .replace(R.id.menu_frame1, new SampleListFragment())
          .commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.mainmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item)
    {
        switch (item.getItemId()) {

          case android.R.id.home:
            menu.toggle();
            break;

          case R.id.github:
            menu.toggle();
            break;

        }
        return super.onOptionsItemSelected(item);

    }

}

1 个答案:

答案 0 :(得分:5)

这是因为您对两个操作使用相同的menu.toggle():根据sources方法打开/关闭左侧菜单。您应该使用showSecondaryMenu()来打开右侧菜单。