我有一个产品列表片段和许多其他片段的活动,我正在尝试使用架构组件导航控制器.
问题是:它取代了(起始目的地)产品列表片段,我不希望在用户单击后退按钮时重新加载列表.
如何使片段事务成为添加而不是替换.
android android-architecture-components android-architecture-navigation
新导航组件的拟议实践在I/O中提供,具有以下模板和建议的理念:
典型的应用程序通常具有包含CollapsingToolbar的详细视图.如何在该架构下构建它?
android android-architecture-components android-jetpack android-architecture-navigation
我正在使用Android Studio 3.2 Canary 14和导航架构组件.使用此功能,您可以像使用Intent一样定义过渡动画.
但是动画被设置为导航图中动作的属性,如下所示:
<fragment
android:id="@+id/startScreenFragment"
android:name="com.example.startScreen.StartScreenFragment"
android:label="fragment_start_screen"
tools:layout="@layout/fragment_start_screen" >
<action
android:id="@+id/action_startScreenFragment_to_findAddressFragment"
app:destination="@id/findAddressFragment"
app:enterAnim="@animator/slide_in_right"
app:exitAnim="@animator/slide_out_left"
app:popEnterAnim="@animator/slide_in_left"
app:popExitAnim="@animator/slide_out_right"/>
</fragment>
Run Code Online (Sandbox Code Playgroud)
为图表中的所有操作定义这一过程非常繁琐!
有没有办法在动作上将一组动画定义为默认值?
我没有运气使用款式.
android android-animation android-jetpack android-architecture-navigation
我正在使用Android Jetpack 的新导航组件.
根Activity设置非常简单:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val navController = findNavController(R.id.navigationFragment)
setupActionBarWithNavController(navController)
bottomNavigationView.setupWithNavController(navController)
}
Run Code Online (Sandbox Code Playgroud)
当导航图中定义了Fragment的标题时,它很有效.但对于一个片段,我想动态设置标题.
我尝试过,findNavController().currentDestination.label = "Hello world"但它没有做任何事情.
我当然可以使用类似的技巧(activity as? AppCompatActivity)?.supportActionBar?.title = "Hello world",但我觉得它会打破对我有用的魔力setupActionBarWithNavController().有没有办法动态更新动作栏标题?
我目前在我的项目中使用 Android 导航架构。它有一个功能,可以使用快捷方式启动任何片段。目前我正在使用 NavController 在单击快捷方式时导航到所需的目的地。但是当我多次单击快捷方式时,每次都会创建一个新的片段实例。所以,我的问题是,有没有办法在使用 NavController 导航到片段时只接受一个片段实例?我在谷歌上搜索了很多次,但一无所获。提前致谢。
我正在尝试在现有应用程序中使用Jetpack的体系结构组件实现导航。
我有一个活动应用程序,其中主要片段(ListFragment)是项目列表。当前,当用户点击列表项时,通过会将第二个片段添加到堆栈中fragmentTransaction.add(R.id.main, detailFragment)。因此,当按下后盖时,DetailFragment会分离,然后ListFragment再次显示。
使用导航架构,这是自动处理的。而不是添加新的片段,而是替换了片段,因此片段视图被销毁,onDestroyView()并onCreateView()在按下back来重新创建视图时被调用。
我知道这是与LiveData和ViewModel一起使用的一种很好的模式,可以避免使用过多的内存,但是在我的情况下,这很烦人,因为该列表的布局很复杂,夸大了时间和CPU的消耗,还因为我需要保存列表的滚动位置并再次滚动到用户离开片段的相同位置。有可能,但似乎应该存在一个更好的方法。
我试图将视图保存在片段的私有字段中,onCreateView()如果已经存在,请重新使用它,但这似乎是一种反模式。
private View view = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.fragment_list, container, false);
//...
}
return view;
}
Run Code Online (Sandbox Code Playgroud)
还有其他更优雅的方法来避免重新布局吗?
android android-fragments android-jetpack android-architecture-navigation android-jetpack-navigation
假设我们有两个片段:MainFragment和SelectionFragment.第二个是用于选择某个对象的构建,例如整数.从第二个片段接收结果有不同的方法,如回调,总线等.
现在,如果我们决定使用导航架构组件导航到第二个片段,我们可以使用以下代码:
NavHostFragment.findNavController(this).navigate(R.id.action_selection, bundle)
Run Code Online (Sandbox Code Playgroud)
其中bundle是Bundle(当然)的实例.正如您所看到的那样,无法访问SelectionFragment我们可以进行回调的位置.问题是,如何使用Navigation Architecture Component接收结果?
android android-architecture-components android-jetpack android-architecture-navigation
我创建了抽屉式导航活动,作为更新的新格式抽屉式导航活动,按照新的Android架构,我得到了它的导航组件结构。
当我单击任何导航项时,下面NavigationView带有NavController和的代码NavigationUI正在打开片段。
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_profile, R.id.nav_privacy_policy,
R.id.nav_terms, R.id.nav_contact_us, 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);
Run Code Online (Sandbox Code Playgroud)
这是为了nav_host_fragment:
<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:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" …Run Code Online (Sandbox Code Playgroud) 我有3个屏幕的工作流程.从"屏幕1"到访问"屏幕2",用户必须接受我在图片"模态"中调用的某些条款和条件.但他只需要接受这些条件一次.下次他在第一个屏幕上时,他可以直接进入屏幕2.用户可以选择不接受这些条款,因此我们返回"屏幕1"并且不要尝试转到"屏幕2".
我想知道如何使用新的导航组件.
以前,我会做什么:
startActivityForResult()并等待模态的结果.将条款标记为已接受.开始"屏幕2"但是使用导航图,无法启动片段来获取结果.
我可以在"模态"屏幕上标记接受的条款,然后从那里开始"屏幕2".问题是,要访问屏幕2,我需要做一个网络请求.我不想复制对API的调用并在"屏幕1"和"模态"中处理其结果.
有没有办法从"模态"回到"屏幕1"与一些信息(用户接受条款),使用Jetpack导航?
编辑:我目前通过使用Yahya建议的相同流程绕过它:使用一个仅用于模态的活动并使用startActivityForResult"屏幕1".我只是想知道我是否可以继续使用导航图来表示整个流程.
即使在添加导航架构组件依赖项后,"新建资源"对话框也没有导航图的导航资源类型
def nav_version = '1.0.0-alpha01'
implementation "android.arch.navigation:navigation-fragment:$nav_version"
implementation "android.arch.navigation:navigation-ui:$nav_version"
Run Code Online (Sandbox Code Playgroud)
navigation android googleio android-architecture-components android-architecture-navigation
android ×10
android-architecture-navigation ×10
android-architecture-components ×4
googleio ×1
navigation ×1