2023年7月13日发(作者:)
BottomNavigationView与Navigation使⽤遇见的⼀个⼩坑BottomNavigationView 与 Navigation使⽤遇见的⼀个⼩坑先说结论吧,只是⼀个⼩问题,⾃⼰记录⼀下。如果⼤家是BottomNavigationView与NavController结合使⽤实现fragment的切换,发现⽆法切换,记得检查⼀下你创建的menu⽂件与navigation⽂件中的id是否是⼀⼀对应。最近在开发⼀个项⽬,因为项⽬的UI⽐较少相对也⽐较简单,考虑⽤⼀单Activity的实现⽅式,使⽤Jetpack的Navigation进⾏页⾯导航,主界⾯的tab实现使⽤⾕歌的BottomNavigationView控件。因为之前写过Demo,所以以为可以信⼿拈来。创建主界⾯的布局,Menu⽂件,以及navigation⽂件主界⾯布局
te(mId(), null, options); return true; } catch (IllegalArgumentException e) { return false; } }继续跟踪在NavController⾥⾯public void navigate(@IdRes int resId, @Nullable Bundle args, @Nullable NavOptions navOptions, @Nullable navigatorExtras) { NavDestination currentNode = y() mGraph : t().getDestination(); if (currentNode == null) { throw new IllegalStateException("no current navigation node"); } @IdRes int destId = resId; final NavAction navAction = ion(resId); Bundle combinedArgs = null; if (navAction != null) { if (navOptions == null) { navOptions = Options(); } destId = tinationId(); Bundle navActionArgs = aultArguments(); if (navActionArgs != null) { combinedArgs = new Bundle(); (navActionArgs); } } if (args != null) { if (combinedArgs == null) { combinedArgs = new Bundle(); } (args); } if (destId == 0 && navOptions != null && UpTo() != -1) { popBackStack(UpTo(), pToInclusive()); return; } if (destId == 0) { throw new IllegalArgumentException("Destination id == 0 can only be used" + " in conjunction with a valid o"); } NavDestination node = findDestination(destId); if (node == null) { final String dest = playName(mContext, destId); if (navAction != null) { throw new IllegalArgumentException("Navigation destination " + dest + " referenced from action " + playName(mContext, resId) + " cannot be found from the current destination " + currentNode); } else { throw new IllegalArgumentException("Navigation action/destination " + dest + " cannot be found from the current destination " + currentNode); } } navigate(node, combinedArgs, navOptions, navigatorExtras); }看到这⾥,⼤家应该明⽩,在NavController⾥⾯,NavDestination会根据destId查找下⼀个跳转的dest,⽽这个destId就是传进来的menuItem的Id。我们在主界⾯的布局⽂件中⽤来展⽰Fragment的宿主控件是"tFragment"
发布者:admin,转转请注明出处:http://www.yc00.com/news/1689216573a222353.html
评论列表(0条)