使用SimonVT的android-menudrawer将滑动菜单添加到活动中

时间:2013-02-23 11:55:32

标签: android slidingdrawer slidingmenu

我真的需要帮助将这种抽屉结合到我的Android应用程序中。

Feinstein滑动菜单抽屉库令人困惑,并导致大量错误,我无法解决 - 尤其是即使在取出Android支持库时jar也不匹配。所以,我切换到了SimonVT的android滑动抽屉库,它似乎更简单(链接here)。

我只需要知道如何处理这些类型的滑动抽屉的人给我说明如何将滑动菜单合并到我的应用程序中。我已经广泛查看了所提供的样本,但它仍然让我感到困惑。我对android编程很新,所以请放心。谢谢!

1 个答案:

答案 0 :(得分:5)

我认为你没有覆盖一些方法...... 我也在我的项目中包含了SimonVTs MenuDrawer(这里是com.example.testapp)。我会发布我的代码,它倾向于WindowSample,希望它有所帮助。 检查您是否已将库添加到项目中(在eclipse中:project-> proberties-> android-> add ...在执行此操作之前,库必须位于您的工作区中)首先从示例应用程序中获取MenuScrollView并将其包含在您的procject源中。然后为这样的侧边栏创建一个布局:

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

<LinearLayout
    android:id="@+id/sidebar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
</LinearLayout>

然后创建一个类,例如名为NavigationManager:

package com.example.testapp;

import net.simonvt.menudrawer.MenuDrawer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NavigationManager extends Activity implements View.OnClickListener {
private static final String STATE_MENUDRAWER = "de.example.testapp.NavigationManager.menuDrawer";
private static final String STATE_ACTIVE_VIEW_ID = "de.example.testapp.NavigationManager.activeViewId";

private MenuDrawer menuDrawer;
private int activeNavigationItemID;
private Context context;
private Activity activity;

@SuppressLint("NewApi")
public void set(int contentView) {
    activity = this;
    context = this;

    menuDrawer = MenuDrawer.attach(activity, MenuDrawer.MENU_DRAG_WINDOW);
    menuDrawer.setContentView(contentView);
    menuDrawer.setMenuView(R.layout.sidebar);

    // add Items to Sidebar, like you want!!!!
    LinearLayout sidebar = (LinearLayout) findViewById(R.id.sidebar);
    for (int i = 1; i <= 5; i++) {
        TextView tv = new TextView(context);
        tv.setText("Item: " + i);
        tv.setId(i);
        tv.setOnClickListener(this);
        sidebar.addView(tv);
    }

    MenuScrollView msv = (MenuScrollView) menuDrawer.getMenuView();
    msv.setOnScrollChangedListener(new MenuScrollView.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            menuDrawer.invalidate();
        }
    });
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH
            && getActionBar() != null) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    TextView activeView = (TextView) findViewById(activeNavigationItemID);
    if (activeView != null) {
        menuDrawer.setActiveView(activeView);
        // mContentTextView.setText("Active item: " + activeView.getText());
    }

}


@Override
protected void onRestoreInstanceState(Bundle inState) {
    super.onRestoreInstanceState(inState);
    menuDrawer.restoreState(inState.getParcelable(STATE_MENUDRAWER));
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelable(STATE_MENUDRAWER, menuDrawer.saveState());
    outState.putInt(STATE_ACTIVE_VIEW_ID, activeNavigationItemID);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        menuDrawer.toggleMenu();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    final int drawerState = menuDrawer.getDrawerState();
    if (drawerState == MenuDrawer.STATE_OPEN
            || drawerState == MenuDrawer.STATE_OPENING) {
        menuDrawer.closeMenu();
        return;
    }

    super.onBackPressed();
}

@Override
public void onClick(View v) {
    // Do something!!!!

    menuDrawer.setActiveView(v);
    menuDrawer.closeMenu();
    activeNavigationItemID = v.getId();
}

}

您必须在标记的行(标有!!!!)上添加您的项目和逻辑,点击后发生的事情。你现在要在你的Acitivities中做的就是让他们扩展NavigationManager并像这样调用set方法:

package com.example.testapp;

import android.os.Bundle;

public class MainActivity extends NavigationManager {

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

    set(R.layout.activity_main);
}
}

希望有所帮助:)