我终于开始使用 Android 版 Compose,但目前陷入导航困境。我有两个问题:
1. 我实现了带有三个按钮的底部导航,其中一个按钮触发嵌套导航图,例如:
HOME --- CONTACTS --- FAVOURITES
|
DETAILS
Run Code Online (Sandbox Code Playgroud)
因此,当我从主页打开详细信息屏幕并切换选项卡时,行为是正确的,嵌套图中的状态和最后目的地会被记住,当我再次点击主页时,它会保留状态并切换回详细信息。但是,如果我在任何其他选项卡上并按返回,它会将我带回到主页,但会重置状态,不会让我返回详细信息。我怎样才能做到这一点?
导航栏:
@Composable
fun BottomNavigationBar(navController: NavHostController) {
BottomNavigation {
val backStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = backStackEntry?.destination?.route
BottomNavigationCollection.items.forEach { navItem ->
BottomNavigationItem(
selected = currentRoute == navItem.route,
onClick = {
navController.navigate(navItem.route) {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
},
icon = {
Icon(
imageVector = navItem.icon,
contentDescription = navItem.title
)
},
label = {
Text(text = navItem.title)
},
)
} …Run Code Online (Sandbox Code Playgroud) 我在 Jetpack Compose 应用程序的项目中设置了一个带有 2 个目的地的底部栏。我尝试遵循谷歌的示例。
例如,它看起来像这样:
@Composable
fun ExampleBottomBar(navController: NavHostController) {
val items = listOf(
BottomNavigationGraph.Graph1,
BottomNavigationGraph.Graph2
)
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
BottomNavigation {
items.forEach { screen ->
BottomNavigationItem(
onClick = {
navController.navigate(screen.route) {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
},
selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
icon = { Icon(imageVector = screen.icon, contentDescription = null) },
label = { Text(stringResource(screen.label)) }
)
} …Run Code Online (Sandbox Code Playgroud) android android-navigation android-architecture-navigation android-jetpack-compose jetpack-compose-navigation
我正在尝试找到一种解决方案来显示多个底部工作表,以适应我们的用户体验/设计。
目前一直在尝试使用Accompanist和Compose Navigation Reimagined,但没有成功。似乎 BottomSheets 的内置内部工作方式一次只能显示,例如显示一张纸、隐藏它并交换内容并显示“新纸”。
问题可能与accompanist 上的这个 github 问题有些相关
Essentially what i'm trying to do is being able to have a "stack" of bottomsheets on top of each other. I've been trying to make things work with the compose navigation, but having issues with background colors and replicating the "bottom sheet" overlay when the screen enters.
Any one who has found a way to implement something along the lines of what i'm trying?
android android-navigation android-jetpack-compose jetpack-compose-navigation jetpack-compose-accompanist
使用向上和向后按钮导航非常复杂(至少对我而言)正如android文档所述,
您可以根据您对详细信息视图的了解,使Up行为更加智能.从上面扩展Play商店示例,假设用户已从上次查看的书籍导航到电影改编的详细信息.在这种情况下,Up可以返回用户之前未导航过的容器(电影). http://developer.android.com/design/media/navigation_between_siblings_market2.png
我正在探索Google IO 2013应用程序的最佳实践,据我所知,上面提到的导航规则没有被考虑在内.这是图片:https: //dl.dropboxusercontent.com/u/6129677/do_not_delete/iosched2013.png
我启动应用程序并打开主屏幕(#1).我点击"浏览会话" - >第二个屏幕打开.然后我点击会话 - 打开详细视图(#3).UP按钮表示这是"Google+"类别,我希望在点击此按钮后导航到"Google+"会话列表.相反,第一个屏幕再次打开(见图).
这是一个正确的行为,我错过了什么?
我一直在我的代码中收到这个警告,我开始担心,因为我接近完成的最后期限.我的主要活动中有一个DrawerLayout,使用Android Studio自己的导航抽屉活动,但由于现在不推荐使用DrawerLayout setDrawerListener,因此必须对代码稍作更改.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Firebase reference to main application URL and 'today's' matches
Firebase mainRef = new Firebase(cons.getFirebaseUrl());
//DrawerLayout settings for the navigation drawer
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.addDrawerListener(toggle);
toggle.syncState();
//Retrieve the navigation drawer and set it's listener for menu item presses
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Run Code Online (Sandbox Code Playgroud)
我收到了关于drawer.addDrawerListener(toggle)和的警告navigationView.setNavigationItemSelectedListener(this)
在我的代码中,我也在这个方法中接收它:
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if …Run Code Online (Sandbox Code Playgroud) 我已经开始使用新的导航组件,而且我真的在挖掘它!但是,我确实有一个问题-当我处于图表的起始目标位置时,应该如何处理后退按钮?
这是我现在使用的代码:
findNavController(this, R.id.my_nav_host_fragment)
.navigateUp()
Run Code Online (Sandbox Code Playgroud)
当我在图表上的任何地方时,它都工作得很好,它使我返回,但是当我在它的开始时,由于后备箱为空,因此应用程序崩溃。
这一切对我来说都很有意义,我只是不确定如何处理。
虽然我可以检查当前片段的ID是否与我知道是图的根的ID相同,但我正在寻找一种更优雅的解决方案,例如是否存在bool标志,以表明当前图中的位置是否为起始位置与否。
有想法吗?
android-navigation android-architecture-components android-architecture-navigation
我有一个活动NavHostFragment。活动收到其意图的某些值。我想将此数据传递到startDestination导航图的第一个片段。我找不到与此有关的任何文档。
我已经在SO上解决了这个问题,但是我似乎找不到的addDefaultArguments方法navController.getGraph()。
可以将捆绑商品传递给startDestination吗?
java android android-navigation android-architecture-components android-architecture-navigation
我有以下内容Activity,这是我的应用程序中的一个:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
PermissionState state = PermissionState.get(this);
if (state == PermissionState.GRANTED) {
getWindow().setBackgroundDrawable(new ColorDrawable(-1));
}
super.onCreate(savedInstanceState);
ActivityMainBinding mainActivityBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
setSupportActionBar(mainActivityBinding.toolbarMain);
NavigationUI.setupWithNavController(mainActivityBinding.toolbarMain, Navigation.findNavController(this, R.id.nav_host_fragment));
}
}
Run Code Online (Sandbox Code Playgroud)
与关联的布局Activity如下,是根据本 Android开发者教程创建的。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
所述 …
android android-navigation android-architecture-components android-jetpack android-architecture-navigation
我试图在导航组件中从导航DialogFragment到Fragment,但是结果很奇怪。
当我从导航DialogFragment到时Fragment,背景片段会更改为目标片段,并在其顶部显示当前对话框,而不仅仅是移动到目标片段。
这是导航图。
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/home"
app:startDestination="@+id/titleScreen">
<fragment
android:id="@+id/titleScreen"
android:name="com.example.android.navigationadvancedsample.homescreen.Title"
android:label="@string/title_home"
tools:layout="@layout/fragment_title">
<action
android:id="@+id/action_title_to_about"
app:destination="@id/aboutScreen"/>
</fragment>
<dialog
android:id="@+id/aboutScreen"
android:name="com.example.android.navigationadvancedsample.homescreen.About"
android:label="@string/title_about"
tools:layout="@layout/fragment_about">
<action
android:id="@+id/action_aboutScreen_to_register"
app:destination="@id/register" />
</dialog>
<fragment
android:id="@+id/register"
android:name="com.example.android.navigationadvancedsample.formscreen.Register"
android:label="fragment_leaderboard"
tools:layout="@layout/fragment_leaderboard" />
</navigation>
Run Code Online (Sandbox Code Playgroud)
为什么我会出现这种现象或如何解决?
修复是指正常的对话框行为。我说,我有一个对话框d上的一个分段的顶部一个,并移动到一个片段乙从一个按钮d,屏幕应该显示乙。当我从B弹出时,它应该转到A之上的D的前一阶段。
android android-fragments android-dialogfragment android-navigation android-architecture-navigation
android ×9
android-architecture-navigation ×5
android-architecture-components ×3
java ×1
jetpack-compose-accompanist ×1
kotlin ×1