Could someone explain to me how this new Navegation Drawer works?

Asked

Viewed 146 times

0

I’m starting to study android, and after update 3.5 of android studio the template of Navegation Drawer has changed a lot and as I’m starting to study,someone could explain to me where the Fragment call is and how I could perform a method by clicking on some Navegation Drawer item.

public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}

}

Galleryfragment

public class GalleryFragment extends Fragment {

private GalleryViewModel galleryViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    galleryViewModel =
            ViewModelProviders.of(this).get(GalleryViewModel.class);
    View root = inflater.inflate(R.layout.fragment_gallery, container, false);
    final TextView textView = root.findViewById(R.id.text_gallery);
    galleryViewModel.getText().observe(this, new Observer<String>() {
        @Override
        public void onChanged(@Nullable String s) {
            textView.setText(s);
        }
    });
    return root;
}

} Galleryviewmodel

public class GalleryViewModel extends ViewModel {

private MutableLiveData<String> mText;

public GalleryViewModel() {
    mText = new MutableLiveData<>();
    mText.setValue("This is gallery fragment");
}

public LiveData<String> getText() {
    return mText;
}

}

1 answer

0

I faced the same problem, actually it is an error of androidstudio in version 3.5. In your activity_main.xml file, make sure that navigationView is before include. If you are, leave it after include that your Drawer will work correctly. To prevent it from happening again you can follow this post here:

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

Browser other questions tagged

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