Problem opening Navigationdrawer

Asked

Viewed 26 times

0

I implemented a NavigationDrawer, but I’m not able to open it.There’s that button on the side of the Activity name. I don’t know if I created it wrong.

Code of Navigationdrawer in my Mainactivity

public class MainActivity extends AppCompatActivity implements  SearchView.OnQueryTextListener,
    MenuItemCompat.OnActionExpandListener{


private DrawerLayout mDrawerLayout;
private ListView mDrawerList;

// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;

private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;

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


    // load slide menu items
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

    navDrawerItems = new ArrayList<NavDrawerItem>();

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], R.drawable.mais_vistos_img));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], R.drawable.recentes_img));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], R.drawable.add_amigo_img));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], R.drawable.historico_img));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], R.drawable.config_icon));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], R.drawable.ajuda_img));

    navMenuIcons.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
    mDrawerList.setAdapter(adapter);

    if (savedInstanceState == null){
        displayView(0);
    }

    private class SlideMenuClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        displayView(position);
    }
}

private void displayView(int position) {
    Fragment fragment = null;
    switch (position) {
        case 0:
            fragment = new MaisVistosFragment();
            break;
        case 1:
            fragment = new RecentesFragment();
            break;
        case 2:
            fragment = new AddAmigoFragment();
            break;
        case 3:
            fragment = new HistFragment();
            break;
        case 4:
            fragment = new ConfigFragment();
            break;
        case 5:
            fragment = new AjudaFragment();
            break;

        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            fragmentManager.beginTransaction()
                    .setCustomAnimations(R.anim.fadein, R.anim.fadeout, R.anim.fadein, R.anim.fadeout)
                    .replace(R.id.frame_container, fragment).commit();
        }
        else {
            fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
        }

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }
    else{
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

XML of NavigatioDrawerin Mainactivity

  <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.uguideufopalfa.MainActivity" >

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#f6f6f6"
        android:choiceMode="singleChoice"
        android:divider="#2d2b28"
        android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>

As it is already very big ask me if you need the class where I create the item, but I think the problem is not there

I don’t know if it’s very relevant, but I’m wearing one TabLayoutwith ViewPager.

1 answer

0


private Toolbar mToolbar;
private ActionBarDrawerToggle mDrawerToggle;
private NavigationView nav_view

private void setupDrawerLayout() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        nav_view = (NavigationView) findViewById(R.id.navigation_view);
        mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, mToolbar, "open", "close"){
            @Override
            public void onDrawerOpened(View drawerView) {


                super.onDrawerOpened(drawerView);
            }
        };
    }

It would look like this ... Oce needs to set your Toolbar

  • vlw for the help, but I decided not to use more the NavigationDrawerbecause I’m already using TabLayout.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.