如何在使用滑动菜单时进入下一个屏幕

时间:2014-03-20 12:03:38

标签: android onclick slidingmenu

我正在使用this库项目来实现滑动菜单,现在我已成功实现它。当我点击按钮它打开的滑动菜单现在我的问题是我如何通过点击菜单选项去任何其他活动。

我希望当我点击My Profile时它会将我带到该屏幕,然后如果我点击Profile Picture然后点击其他屏幕......等等...

以下是我的代码。

public class MainActivity extends Activity {

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

        final SlidingMenu menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.RIGHT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setFadeDegree(0.5f);
        menu.attachToActivity(MainActivity.this, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.activity_menu);

        Button mButton = (Button) findViewById(R.id.slidingMenu);
        mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                menu.showMenu();
            }
        });
    }

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

修改

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

    <RelativeLayout
        android:id="@+id/settingMenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#262E38"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/setting"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/settings" />

        <ImageView
            android:id="@+id/myprofile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/setting"
            android:src="@drawable/myprofile" />

        <ImageView
            android:id="@+id/profilepicture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/myprofile"
            android:src="@drawable/profilepicture" />

        <ImageView
            android:id="@+id/changewallpaper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/profilepicture"
            android:src="@drawable/changewallpaper" />

        <ImageView
            android:id="@+id/notification"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/changewallpaper"
            android:src="@drawable/notofication" />

        <ImageView
            android:id="@+id/comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/notification"
            android:src="@drawable/commentstrip" />

        <ImageView
            android:id="@+id/post"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/comment"
            android:src="@drawable/post" />

        <ImageView
            android:id="@+id/chat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/post"
            android:src="@drawable/chat" />

        <ImageView
            android:id="@+id/likedislike"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/chat"
            android:src="@drawable/likeanddislike" />

        <ImageView
            android:id="@+id/privacypolicy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/likedislike"
            android:src="@drawable/privacypolicy" />

        <ImageView
            android:id="@+id/termscondition"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/privacypolicy"
            android:src="@drawable/termscondition" />

        <ImageView
            android:id="@+id/contactus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/termscondition"
            android:src="@drawable/contactus" />

        <ImageView
            android:id="@+id/logout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/contactus"
            android:src="@drawable/logout" />
    </RelativeLayout>

</ScrollView>

4 个答案:

答案 0 :(得分:1)

嗯,这是一个快速的伪代码,你可以做其余的事。

@Override
public boolean onOptionsItemSelected(MenuItem item) {

      switch(item.getItemId()){
       case R.id.profile:
        profile();
        break;
       case R.id.logout:
        logout();
        break;
     }
 return super.onOptionsItemSelected(item);
}

答案 1 :(得分:0)

使用以下代码

 View op1 = findViewById(R.id.op1);
 op1.setOnClickListener(new OnClickListener() 
 {
  startActivity(this, YourActivity.class);
 }}
 );

答案 2 :(得分:0)

private void displayView(int position) 
{
        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new MyProfile();
            Toast.makeText(getApplicationContext(), ""+position, Toast.LENGTH_SHORT).show();
            break;
        case 1:
            fragment = new ProfilePicture();
            Toast.makeText(getApplicationContext(), ""+position, Toast.LENGTH_SHORT).show();
            break;

        default:
            break;
        }

Refer this link

使用开关案例..使用位置一旦您单击一个项目,它将带您到该特定类。

答案 3 :(得分:0)

您还应该从Activity Class覆盖onOptionsItemSelected方法。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.myProfile:
        showMyProfile();
        return true;
    case R.id.profilePicture:
        profilePicture();
        return true;
    case R.id.changeWallpaper:
        changeWallpaper();
        break;
    default:
        return super.onOptionsItemSelected(item);
    }

然后您可以创建相应的方法来初始化您的意图

public void profilePicture() {
Intent intent = new Intent(this,ProfilePicture.class);
startActivity(intent);