Reputation: 365
I really need help incorporating this type of drawer into my android application.
The Feinstein sliding menu drawer library is confusing and leads to tons of errors that I'm unable to fix - especially jar mismatches even when the android support library is taken out. So, I switched to SimonVT's android sliding drawer library which seems much simpler (linked here).
I just need someone who knows how to deal with these type of sliding drawers to give me instructions on how to incorporate a sliding menu into my application. I've looked extensively at the samples provided and it's still confusing for me. I'm very new to android programming, so please go easy. Thank you!
Upvotes: 1
Views: 5301
Reputation: 680
I think you haven't override some Methods... I included SimonVTs MenuDrawer at my project too (here com.example.testapp). I'll poste my code, it leans against the WindowSample, hope it help. Check if you have add the library to your project (in eclipse: project->proberties->android->add... before doing that, the library must be in your Workspace) First take the MenuScrollView from the example App and include it to your procject source. Then create a layout for the sidebar like this:
<?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>
Then create a class, e.g. named 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();
}
}
You have to add your items and the logic, what happend after a click, on the marked lines (marked with !!!!). All you have to do in your Acitivities now is to let them extend the NavigationManager and call the set Method like this:
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);
}
}
Hope it help :)
Upvotes: 5