Android How to modify the contents of a Drawer Menu from a Fragment?

1

Good morning.

I have an activity with a drawer menu that has sections, what I want to do is modify the text of those sections from a fragment, but I do not know how the instance should be created in my fragment to be able to modify the contents of my drawer menu.

Thank you in advance.

Here my fragment.

public class ActionFragment extends Fragment {

private Button btnCambiarTexto;

public ActionFragment() {
    // Required empty public constructor
}


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

    btnCambiarTexto = (Button) v.findViewById(R.id.btnCambiarTexto);

    btnCambiarTexto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //al presionar cambiar texto de la seccion email, que aparece en la cabecera del drawer menu
        }
    });

    return v;
}}

here my drawer menu (Activity)

public class LeftMenuActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

private TextView txtSearchToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_left_menu);


    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);

    //seleccionamos por defecto la primera opcion del menu
    navigationView.getMenu().getItem(0).setChecked(true);
    onNavigationItemSelected(navigationView.getMenu().getItem(0));

}}
    
asked by devjav 04.11.2016 в 17:09
source

1 answer

1

To update the elements within a Drawer menu you simply have to update the Adapter with the new data and finally assign the Adapter with the new data to your menu.

 //Notifica actualización de datos.
 myAdapter.notifyDataSetChanged();
 //Asigna adapter.
 mDrawerList.setAdapter(myAdapter);
    
answered by 04.11.2016 в 17:46