没有按钮的滑动菜单

时间:2013-10-30 00:58:49

标签: android android-arrayadapter slidingmenu

我需要帮助解决一些我想弄清楚的代码。我的项目包括一个在YouTube,Google +,Facebook等中使用的拉出式菜单或幻灯片菜单。就像那些应用程序一样,我想用手指从左向右滑动以显示菜单。我现在如何拥有它是一个按钮,你必须按,以便看到我有点不想要的菜单。无论如何,这是代码:

主要活动:

public class MainActivity extends Activity 
{
private ListView list;
private ArrayAdapter<String> adapter;
private RibbonMenuView rbmView;
private Button test;
private ListView rbmListView, rbmListView2;
private ArrayAdapter<String> adapt, adapter2;

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

    /** Init our views **/
    rbmView = (RibbonMenuView) findViewById(R.id.ribbonMenuView1);
    list = (ListView) findViewById(R.id.listView1);

    /** Calls "hideMenu()" after each click similiar to the Facebook or Google+ application **/
    test = (Button) findViewById(R.id.button);
    test.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            // shows how to hide the menu
            if (rbmView != null)
            {
                rbmView.hideMenu();
            }
        }
    });

    /** This is the most important ListView, updating the main list in the Activity **/
    final String[] items_list = { "Shop1", "Shop2", "Shop3" };
    adapt = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items_list);
    rbmListView = (ListView) findViewById(R.id.rbm_listview);
    if (rbmListView != null)
    {
        rbmListView.setOnItemClickListener(new OnItemClickListener() 
        {
            /*
             * (non-Javadoc)
             * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
             */
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            {
                switch (position)
                {
                case 0:
                    String[] items = { "Location", "Contact", "Appointment" };
                    adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items);
                    break;

                case 1:
                    String[] items1 = { "Location", "Contact", "Appointment" };
                    adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items1);
                    break;

                case 2:
                    String[] items2 = { "Location", "Contact", "Appointment" };
                    adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items2);
                    break;
                }

                // always update these
                if (list != null)
                {
                    list.setAdapter(adapter);
                    list.setClickable(true);
                    list.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            switch (i)
                            {
                                case 0: // Location
                                    startActivity(new Intent(getBaseContext(), LocationActivity.class));

                                    break;
                                case 1: // Contact
                                    ;startActivity(new Intent(getBaseContext(), ContactActivity.class));

                                    break;
                                case 2: // Appointment
                                    startActivity(new Intent(getBaseContext(), AppointmentActivity.class));

                                    break;
                            }
                        }
                    });
                }

                if (rbmView != null)
                {
                    rbmView.hideMenu();
                }
            }
        });
        rbmListView.setAdapter(adapt);
    }

    /** This is the second ListView on the menu **/
    final String[] items_list2 = { "About", "Deals", "Contact Developer", };
    adapter2 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items_list2);
    rbmListView2 = (ListView) findViewById(R.id.rbm_listview2);
    if (rbmListView2 != null)
    {
        rbmListView2.setOnItemClickListener(new OnItemClickListener()
        {
            /*
             * (non-Javadoc)
             * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
             */
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                switch (position)
                {
                    case 0:
                        startActivity(new Intent(MainActivity.this, About.class));
                        break;

                    case 1:
                        startActivity(new Intent(MainActivity.this, DealsCode.class));
                        break;

                    case 2:
                        startActivity(new Intent(MainActivity.this, ContactDev.class));
                        break;
                }
                /*Toast.makeText(MainActivity.this, items_list2[position], Toast.LENGTH_SHORT).show();
                if (rbmView != null)*/
                {
                    rbmView.hideMenu();
                }
            }
        });
        rbmListView2.setAdapter(adapter2);
    }
}

/**  Allows users, even API < 5, to use the back button **/
/*
 * (non-Javadoc)
 * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
 */
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    switch (keyCode)
    {
    case KeyEvent.KEYCODE_BACK:
        // if view is showing, close it
        // all other back button clicks are handled by the phone
        if (rbmView != null)
        {
            if (rbmView.isMenuShowing())
            {
                new Handler().postDelayed(new Runnable()
                {
                    public void run()
                    {
                        rbmView.hideMenu();
                    }
                }, 50);
            } 
            else
            {
                return super.onKeyDown(keyCode, event);
            }
        }
        break;
    default:
        return super.onKeyDown(keyCode, event);

    }

    return true;
}

/*
 * (non-Javadoc)
 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
 */
public boolean onCreateOptionsMenu(Menu menu) 
{
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

/*
 * (non-Javadoc)
 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
 */
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch (item.getItemId()) 
    {
    case R.id.test:
        // toggle our menu
        if (rbmView != null)
        {
            rbmView.toggleMenu();
        }
        break;

    default:
        return super.onOptionsItemSelected(item);

    }

    return true;
}
}

拉出式菜单/滑动式菜单:

 public class RibbonMenuView extends LinearLayout 
 {
private LinearLayout menu;

/**
 * Constructor to create menu
 * @param context Context
 * @param attrs Other Attributes
 */
public RibbonMenuView(Context context, AttributeSet attrs) 
{
    super(context, attrs);
    LayoutInflater.from(context).inflate(R.layout.rbm_menu, this, true);
    menu = (LinearLayout) findViewById(R.id.menu);
}

/**
 * Show our Menu
 */
public void showMenu()
{
    if (menu != null)
    {
        new Handler().postDelayed(new Runnable()
        {
            public void run()
            {
                menu.setVisibility(View.VISIBLE);   
                menu.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.rbm_in_from_left));
            }
        }, 50);
    }
}

/**
 * Hide our Menu
 */
public void hideMenu()
{
    if (menu != null)
    {
        new Handler().postDelayed(new Runnable()
        {
            public void run()
            {
                menu.setVisibility(View.GONE);
                menu.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.rbm_out_to_left));
            }
        }, 50);
    }
}

/**
 * Toggle the Menu open/close
 */
public void toggleMenu()
{
    if (menu != null)
    {
        if (menu.getVisibility() == View.GONE)
        {
            showMenu(); 
        } 
        else
        {
            hideMenu();
        }
    }
}

/**
 * Check to see if the menu is visible
 * @return Visible
 */
public boolean isMenuShowing()
{
    if (menu != null)
    {
        return menu.getVisibility() == View.VISIBLE;
    }

    return false;
}
}

告诉我是否还需要其他任何东西!

0 个答案:

没有答案