我想实现这样的导航(下图),其中片段A,B,C,D表示在应用程序的信息层次结构中向下导航.
在电话上只能看到一个片段,在平板电脑上有两个片段可见.
我见过的所有教程和文档(例如this和this)都提到了Master-Detail视图,左边有一个主片段,右边有一个详细视图,但似乎没有人指定如何钻从细节更深入.

事实上,平板电脑的GMail应用程序可以作为我自己的应用程序.假设片段A是帐户和文件夹列表,片段B是电子邮件列表,片段C是会话本身.
该屏幕是具有片段A和B的配置
该屏幕具有片段B和C.

问题是:我应该用一个Activity来实现它,其中
如果是这样,我将如何插入新片段?在FragmentTransaction中,我有两个可用的操作 - add()和replace().
add(),那么下面的片段似乎还活着(它们没有进入暂停状态)并保持完整状态(但可能会浪费一些资源)(因此在片段D中,所有C,B和A仍将存在),在后退按钮上,滚动状态和加载的数据仍然存在replace(),那么之前的片段将被移除,因此在任何时刻顶部将只有一个(在电话上)片段,并且在后退按钮上交易将被反转,旧片段被添加回去,但它赢了不记得以前的状态这些选项对我来说都不合适.教程在FragmentTransaction中使用replace() - 但是它们如何恢复前一个片段的状态,例如在GMail应用程序中,电话片段B从服务器加载无限滚动,然后当我用片段C替换它时然后回去,B应该出现在它停止的地方,对吧?
我真的不知道该做什么或在哪里看,请指教.
我创建了导航抽屉并在列表中显示项目.我的列表是静态的,但我正在显示点数.因此,当用户选择列表中的项目并执行某些活动时,他会获得积分.所以我想尽快更新积分.我怎样才能做到这一点./
以下是我的代码:
活动类:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_page);
// Initializing
dataList = new ArrayList<DrawerItem>();
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
sharedPreferences = getSharedPreferences(Constant.LOGIN_CHECK,
Constant.PRIVATE_MODE);
editSharedPreferences = sharedPreferences.edit();
if (sharedPreferences.getBoolean(Constant.IS_ALREADY_LOGIN,
false)) {
loginInBackground();
}
String p1 = sharedPreferences.getString(Constant.USERS_POINTS,
"");
String p2 = sharedPreferences.getString(
Constant.LEVEL_ONE_POINTS, "");
String p3 = sharedPreferences.getString(
Constant.LEVEL_TWO_POINTS, "");
String pointText = " POINTS";
dataList.add(new DrawerItem("OVERVIEW", R.drawable.ic_action_eye,
Color.DKGRAY, Color.TRANSPARENT));
dataList.add(new DrawerItem("EARN POINTS", R.drawable.ic_action_dollor,
Color.GREEN, Color.TRANSPARENT));
dataList.add(new DrawerItem("MY …Run Code Online (Sandbox Code Playgroud) android android-listview navigation-drawer android-navigation
以我正在处理的应用程序为例: - 它有一个包含多个项目的navigationDrawer; 现在有两件我感兴趣的项目,我称之为X和Y.
单击X和Y时,将显示包含x元素或y元素列表的片段
选择和x或y列表元素显示一个新片段,其中我显示有关选择项的信息; 视图片段对于x和y元素是不同的
在视图片段中,我可以选择编辑带来编辑片段的特定元素
片段方法正在运行,但我花了一些时间来管理片段之间的导航.此外,我可能不得不在抽屉中添加一些类似于X和Y的新项目.我的主要活动,我有抽屉和我进行片段切换,已经非常密集,这让我想到了:应该我从片段切换到活动?我正在考虑在选择抽屉项目时启动新活动,并处理与该活动中所选项目相关的列表/视图/编辑片段,而不是处理单个活动中所有项目的所有片段.
这是个好主意吗?这是不好的设计?
android android-fragments navigation-drawer android-navigation
我已经安装了最新的金丝雀版Android Studio,并按照此(https://developer.android.com/topic/libraries/architecture/navigation/navigation-implementing)说明实现了简单的两页导航.基本上page1有一个按钮,当它被点击时,应用程序显示page2.
它有效,但有一个问题......它似乎没有自动对动作栏做任何事情.是否应该通过导航库自动显示/返回箭头和操作栏上的"标签"属性?或者我应该像以前一样手动完成所有工作?我想在第2页显示时显示后退箭头和动作(工具)栏上的"详细信息".
在按钮上单击第1页.
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{
button1.setOnClickListener {
val nav = NavHostFragment.findNavController(this);
nav.navigate(R.id.show_page2)
}
}
Run Code Online (Sandbox Code Playgroud)
主要活动XML.默认情况下,它是默认的操作栏,我用ToolBar替换它.没有区别.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:background="?attr/colorPrimary"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_width="match_parent">
</androidx.appcompat.widget.Toolbar>
<fragment
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:navGraph="@navigation/nav_graph"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
导航图XML.
<?xml version="1.0" encoding="utf-8"?>
<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/nav_graph"
app:startDestination="@id/page1">
<activity
android:id="@+id/mainActivity2"
android:name="com.android.navtest.MainActivity"
android:label="activity_main"
tools:layout="@layout/activity_main"/>
<fragment
android:id="@+id/page1"
android:name="com.android.navtest.BlankFragment2"
android:label="Home page"
tools:layout="@layout/page1">
<action
android:id="@+id/show_page2" …Run Code Online (Sandbox Code Playgroud) android android-navigation android-jetpack android-architecture-navigation
我有2个动作
动作1
<action
android:id="@+id/actionBaseFragmentToAskForLocation"
app:destination="@+id/introAskForLocationFragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
Run Code Online (Sandbox Code Playgroud)
动作2
<action
android:id="@+id/actionIntroAskLocationToLogin"
app:destination="@id/loginFragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_right"
app:popExitAnim="@anim/fade_out"
app:popUpTo="@+id/app_main_navigation" />
Run Code Online (Sandbox Code Playgroud)
我想要的是当第二个动作被触发时,我想清除后堆栈并仅将loginFragment设置为保留在堆栈中。
唯一的问题是当我执行Action2时,将“ slide_out_right”作为退出动画执行
我知道,如果我们从堆栈中弹出片段,则将触发action1的“ popExitAnim”,而不是action2的“ exitAnim”。
但是我想知道如何让片段执行slide_out_left动画以退出并同时将其弹出堆栈。
android android-navigation android-architecture-components android-architecture-navigation
如何使用导航架构组件 safeargs 将参数传递给对话框片段?下面是我当前的实现
开始片段
val navController = findNavController()
val action =
QuestionListFragmentDirections.actionQuestionListFragmentToCustomDialogFragment(args.templateFlag)
navController.navigate(
action
)
Run Code Online (Sandbox Code Playgroud)
目的地片段
args.templateFlage //supposed to return a boolean value
//but throws java.lang.IllegalStateException: Fragment QuestionTypeDialogFragment{e8be5e1}
(47b305ea-35b2-49e0-b378-d31a08ba9a41) QuestionTypeDialogFragment} has null arguments
Run Code Online (Sandbox Code Playgroud) 是否可以在 Android/Jetpack Compose 中预填充导航后台?
我有一个深层链接,可以深入导航层次结构,但是,按后退时,它会导航到根路线。
例子:
Route.Main -> Route.List -> Route.Details(参数:id)
深层链接: https: //mywebsite.com/details/id
当前行为:它使用正确的参数打开 Route.Details,但是,在 onBack 时,它打开 Route.Main
期望的行为:它应该打开 Route.List
我知道我可以手动“编程”此行为,但我更喜欢“配置”它。
android android-navigation android-jetpack-compose jetpack-compose-navigation
我刚刚开始使用Android,我按照本指南将向上导航添加到操作栏上的活动.
http://developer.android.com/training/implementing-navigation/ancestral.html
然而,当我使用动作栏中的向上按钮时,它的转换使其显示为打开新活动的淡入过渡,而不是它应该的淡出,就像按下按钮上的后退按钮时一样.底部.
我该怎么做才能让它显示正确的过渡?谢谢.
由于 android 从 Android 设置导航回来的方式,我的用户体验不一致。
在我的应用程序中,用户需要授予我的应用程序访问权限ACTION_USAGE_ACCESS_SETTINGS,我通过以下方式访问:
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
在为我的应用程序将设置切换为打开后,我需要用户返回到我的应用程序。据我所知,这样做的唯一方法是让他们按下手机上的后退按钮(很想知道是否可以在切换设置后自动返回!!!?!)。
现在将发生两件事之一:
1) 用户最近没有使用android设置,所以它没有打开(即在打开的应用程序抽屉中打开)。第一次按下后退按钮将根据需要将它们带到我的应用程序。
2) 用户最近使用过安卓设置。因此设置已经在应用程序抽屉中打开。现在,当用户按下返回键时,Android 会将他们带回他们最近使用过的每个设置页面(即后退按钮带他们查看他们在 android 设置页面中的历史记录)。可能需要按 2、3 或 4 次后退按钮才能离开 Android 设置并返回到我的应用程序。这显然是糟糕的 UI/UX,我想知道是否有更好的方法?
我注意到在安装 Google 应用程序时,将设置切换为 ON 后,它会自动退出并返回到调用该设置的应用程序。能够做到这一点是理想的,但我无法解决。
谢谢!
我正在使用 Android Jetpack 导航组件。我有一个带有 id 的嵌套导航图,比如说嵌套图R.id.nested_graph
的第一个Fragment接收一个参数。
<navigation
android:id="@+id/nested_graph"
android:label="Nested Graph"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="...."
android:label="....">
<argument
android:name="item_id"
app:argType="integer" />
</fragment>
[...]
</navigation>
Run Code Online (Sandbox Code Playgroud)
如何使用安全 args将参数传递给嵌套图?
目前,我需要在包中手动传递参数,使用直接接收嵌套图的 id 的 API:
val args = Bundle()
args.putInt("item_id", itemId)
navController.navigate(R.id.nested_graph, args)
Run Code Online (Sandbox Code Playgroud)
我想使用安全参数,并执行以下操作:
val directions = OrigininFragmentDirections.nestedGraph(itemId)
navController.navigate(directions)
Run Code Online (Sandbox Code Playgroud)
但是在尝试时,我在构建时收到以下错误:
Too many arguments for public final fun nestedGraph(): NavDirections defined
Run Code Online (Sandbox Code Playgroud)
问题是导航图预处理正在生成工厂方法来创建没有签名中所需参数的NavDirections对象。
嵌套图的声明如下所示:
android android-navigation android-jetpack-navigation android-navigation-graph