滑出菜单ListView问题

时间:2013-10-18 02:24:10

标签: android android-layout listview

我遇到了一些我似乎无法解决的代码部分问题。当我点击Shop1时,ListView中会出现三个项目,即位置,联系人和约会。当我选择其中一个时,我想知道如何进入另一个活动。例如,如果我想点击位置,我希望它带我到另一个布局文件。帮助会很棒!

以下是一些照片:

Not Clicked

未点击

enter image description here

已点击

enter image description here

点击位置转到其他活动

以下是代码:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;


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;


                default:
                    String[] itemsd = { "A", "B", "C", "D" };
                    adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, itemsd);
                    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 ((int)l)
                            {
                                case 0: // Location
                                    //Intent intent = new Intent(getApplicationContext(), NEW_ACTIVITY.class);
                                    //startActivity(intent);
                                    break;
                                case 1: // Contact
                                    //Intent intent = new Intent(getApplicationContext(), NEW_ACTIVITY.class);
                                    //startActivity(intent);
                                    break;
                                case 2: // Appointment
                                    //Intent intent = new Intent(getApplicationContext(), NEW_ACTIVITY.class);
                                    //startActivity(intent);
                                    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;
}
}

如果还有其他需要请告诉我!

1 个答案:

答案 0 :(得分:1)

尝试使用startActivity(new Intent(getBaseContext(), activityToStart.class));

为什么要将作为ID的float转换为int?你应该只使用作为位置的int值。

     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;
                            }
                        }
                    });

如果您知道单击项目的“行ID”,则可以使用浮点值。