Problem opening NavigationDrawer

0

I've implemented a NavigationDrawer , but I'm not able to open it. That button does not appear on the side of the Activity name. I do not 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 NavigatioDrawer in 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's too old, ask me if you need the class where I create the item, but I do not think the problem is there.

I do not know if it's very relevant, but I'm using TabLayout with ViewPager .

    
asked by anonymous 04.10.2017 / 04:28

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 something like this ... you need to set your toolbar

    
04.10.2017 / 04:45