简体   繁体   English

使用库中的片段活动使用底部 NAV 栏更改片段

[英]Changing fragment using bottom NAV bar using fragment activity in a library

I start a new Android project with a bottom NAV bar in the mainactivity in the app module as in the picture.我开始一个新的 Android 项目,在 app 模块的 mainactivity 中有一个底部 NAV 栏,如图所示。 I plan to set the initial fragment in the featuretab1 module.我打算在 featuretab1 模块中设置初始片段。

I found way to change activity using intent and setting the dependency.我找到了使用意图和设置依赖来改变活动的方法。 Just want to confirm, to change the fragment, just need to put below coding.只是想确认一下,要更改片段,只需要在下面编码即可。

//Java in featuretab1
public class featuretab1fragment extend fragment {

    //fragment detail

}

//navbar.setOnClicklistener action in app module main activty
Fragment f = new featuretab1fragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, f);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();

Is it what i need to do?这是我需要做的吗?

想要达到

Use getSupportFragmentManager() instead of getFragmentManager() for compatibility across devices lower than API 14. Check the official docs before asking this question.使用getSupportFragmentManager()而不是getFragmentManager()以实现 API 14 以下设备的兼容性。在问这个问题之前检查官方文档。 Here is the link to it: https://developer.android.com/guide/components/fragments#java这是它的链接: https : //developer.android.com/guide/components/fragments#java

Fragment newFragment = new featuretab1fragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.nav_host_fragment, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

Since you are just starting out with Android development, I would recommend you to learn Kotlin or Flutter instead.由于您刚刚开始使用 Android 开发,我建议您改用 Kotlin 或 Flutter。 Both are better than Java in terms of making Android apps.在制作 Android 应用程序方面,两者都优于 Java。 Kotlin requires less code to do the same thing as what Java does and it has null safety. Kotlin 需要更少的代码来完成与 Java 相同的事情,并且它具有空安全性。 Flutter can create both android and ios apps at the same time with a single codebase. Flutter 可以使用单个代码库同时创建 android 和 ios 应用程序。 Flutter uses the dart programming language. Flutter 使用 dart 编程语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM