my image
Hello guys
I use templater Bottom Navigation Activity and dont know how to scroll to top RecyclerView in my Home Fragment
My MainActivity code:
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
The question is quit old, but maybe it helps others.
This is the code you have to put in the fragment (in Kotlin):
val nav = activity?.findViewById<BottomNavigationView>(R.id.bottom_navigation)
nav?.setOnItemReselectedListener { item ->
if (item.itemId == R.id.mainFragment) {
recyclerView.smoothScrollToPosition(position)
}
}
Related
I have setup the toolbar title in NavGraph with
`private void setUpNavigationToolbar(){
final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.container_fragment;
NavController navController = navHostFragment.getNavController();
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph()).build();
NavigationUI.setupWithNavController(
toolbar, navController, appBarConfiguration);
}`
and toolbar title is set using
android:label="Sample title" inside the Nav graph
When the fragment is launched the title appears and disappear in a moment.
I am looking for a solution for the toolbar title persists even after launching the fragment
I am using navigation-compose along with bottom bar in jetpack compose. I want to show different bottom bar for different type of user. For that, I need to set conditional startDestination in NavHost. How do i do that?
I've tried below but it change startDestination but does not reflect in UI.
val user by remember { mutableStateOf(viewModel.user) }.collectAsState()
var startDestination = Screens.UserType1.route
LaunchedEffect(user) {
startDestination = if (loggedInUser?.userType == UserType.Seller) Screens.SellerHome.route else Screens.BuyerHome.route
}
While below code throws java.util.NoSuchElementException: List contains no element matching the predicate.
when (user?.userType) {
UserType.Seller -> {
startDestination = Screens.SellerHome.route
}
UserType.Buyer -> {
startDestination = Screens.BuyerHome.route
}
else -> {}
}
My NavHost
NavHost(
modifier = modifier,
navController = navController,
startDestination = startDestination
) {
...
}
Change this
var startDestination = Screens.UserType1.route
To this
var startDestination by remember { mutableStateOf(Screens.UserType1.route) }
When opening when I click on the bottom navigation elements, the screens take a long time to load, I don’t know what the problem is
Here is the bottom navigation code:
bottomBar = {
BottomNavigationBar(
items = bottomNavItemModels,
navController = navController,
onItemClick = {
navController.navigate(it.route) {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
}
}
)
}
) {
Navigation(
navHostController = navController,
state = modalBottomSheetState
)
}
In this guide, it says:
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph())
.setDrawerLayout(drawerLayout)
.build();
but it does not say how navController is obtained. In the next paragraph, it says
call setupWithNavController() from your main activity's onCreate() method, as shown below:
NavHostFragment navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment);
NavController navController = navHostFragment.getNavController();
NavigationView navView = findViewById(R.id.nav_view);
NavigationUI.setupWithNavController(navView, navController);
which indicates that the 2 snippets are in different files. So where should I place the first snippet (initializing appBarConfiguration)? Currently, I put them all in my main activity, and it seems to work fine.
you have to attach the appBarConfiguration to the NavigationUI with NavController using setupActionBarWithNavController() method. Code below shows an example,
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
setupWithNavController() method is to attach NavigationView to NavigationUI
im trying to remove the hamburguer icon because I already have one in my custom Toolbar, for remove it I have this on the activity:
Toolbar toolbar = findViewById(R.id.toolbar_main);
setSupportActionBar(toolbar);
final 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.navigation_dieta, R.id.navigation_calendario, R.id.navigation_alimentos, R.id.navigation_perfil)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav_view);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
toolbar.setNavigationIcon(null);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
drawerIcon = findViewById(R.id.toolbar_main_menu_icon);
drawerIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawer.openDrawer(GravityCompat.START);
}
});
The icon is hidden at startup, but when I select a menu item it reappears.
After more than 10 hours I found the answer, just delete this:
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);