Fragment with drawermenu in the action toolbar

1

The problem is the following I have my main activiy that is composed of a drawer activity when I give to one of the options of the drawer activity I have to pass to a fragment , the problem is the following when I pass to fragment I lose the option to open the drawer menu from fragment

    import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        FragmentManager fragmentManager = getSupportFragmentManager();



        if (id == R.id.nav_trigonometria) {

            fragmentManager.beginTransaction().replace(R.id.cont, new Fragmen1()).commit();

        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_conversor) {

            fragmentManager.beginTransaction().replace(R.id.cont, new Fragmen2()).commit();

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

that is the java code of the main activity

This is the fragment code where I want to implement the drawer

 public class Fragmen1 extends Fragment {

   Button HBoton;
   Button GBoton;
   Button SCTBoton ;
   Button RGboton ;
   Button CPBoton ;
   Button ARBoton;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fragmen1, container, false);


        HBoton = (Button) view.findViewById(R.id.HipotenusaBoton);
        HBoton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                Intent HBoton = new Intent(getActivity(), Hipotenusa.class);
                startActivity(HBoton);
            }
        });



         GBoton= (Button) view.findViewById(R.id.button3);
        GBoton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                Intent GBoton = new Intent(getActivity(), Grados_Radianes.class);
                startActivity(GBoton);
            }
        });



        SCTBoton = (Button) view.findViewById(R.id.SCTboton);
        SCTBoton.setOnClickListener(new  View.OnClickListener() {


            @Override
            public  void  onClick (View v ){



                Intent SCTBoton = new Intent(getActivity(), SenoCosenoTangente.class);
                startActivity(SCTBoton);
            }
        });

        RGboton = (Button) view.findViewById(R.id.button6);
        RGboton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent RGboton = new Intent(getActivity(),RadianesGrados.class);
                startActivity(RGboton);
            }
        });


        CPBoton = (Button) view.findViewById(R.id.CAPbutton);
        CPBoton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent CPBoton = new Intent( getActivity(),CordenadasRectagularesPolares.class);
                startActivity(CPBoton);
            }
        });

        ARBoton = (Button) view.findViewById(R.id.arboton);
        ARBoton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent ARBoton = new Intent( getActivity(),AnguloReferencia.class);
                startActivity(ARBoton);

            }
        });

        return  view;

    }

}
    
asked by Liantony Pozo 14.04.2017 в 22:19
source

1 answer

1
  

"This is the fragment code where I want to implement the drawer"

The NavigationDrawer should usually be in the Activity that contains the Snippets, this to only replace the content.

In the event that you do not want the Navigation Drawer in any Fragment, simply by performing the transaction of this, you disable the Navigation Drawer .

    
answered by 14.04.2017 в 23:26