我认为你没有覆盖一些方法......我也在我的项目中包含了 SimonVTs MenuDrawer(这里是 com.example.testapp)。我会发布我的代码,它靠在 WindowSample 上,希望对您有所帮助。检查您是否已将库添加到您的项目中(在 Eclipse 中:project->proberties->android->add... 在此之前,该库必须在您的工作区中)首先从示例应用程序中获取 MenuScrollView 并包含它到您的项目源。然后为侧边栏创建一个布局,如下所示:
<?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);
}
}
希望它有帮助:)