我正在尝试从Android Architecture Components实现Nagivation。我能够成功浏览片段。我正在尝试将其附加到我的底部导航中,但无法正确使用它。我可以使用Navigation.findNavController(View).navigate(R.id.Fragment)
成功地在片段之间导航,但是当我使用任何UI组件或后退按钮进行导航时,底部导航中的高亮显示并没有发生变化,如下图gif所示。
我对MainActivity.kt
的代码如下,
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
navController.navigate(R.id.homeFragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_dashboard -> {
navController.navigate(R.id.addEmotionFragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_notifications -> {
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navController = Navigation.findNavController(this, R.id.nav_host)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}
}
对于HomeFragment.kt
class HomeFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
button.setOnClickListener {
Navigation.findNavController(it).navigate(R.id.addEmotionFragment)
}
}
}
答案 0 :(得分:4)
尝试使用导航用户界面。
implementation 'androidx.navigation:navigation-ui:' + navigationVersion //currently 1.0.0-alpha05
活动
navController = Navigation.findNavController(this, R.id.nav_host)
NavigationUI.setupWithNavController(bottomNavigation, navController)
并确保您的片段ID与菜单ID相匹配。
<item
android:id="@+id/homeFragment"
android:title="Home"/>
答案 1 :(得分:1)
您需要在按钮内部手动设置选中的属性,例如:
DSLContext ctx;
String createTableSql = ctx.createTable("TABLE_NAME")
.column("TABLE_ID", SQLDataType.BIGINT.identity(true))
.column("COL_1", SQLDataType.VARCHAR(64).nullable(false))
.constraints(
DSL.constraint("PK_TABLE").primaryKey("TABLE_ID"),
DSL.constraint("UNIQUE_COL_1").unique("COL_1")
).getSQL();
System.out.println(createTableSql);
Java代码为:
create table `TABLE_NAME`(
`TABLE_ID` bigint not null auto_increment,
`COL_1` varchar(64) not null,
constraint `PK_TABLE` primary key (`TABLE_ID`),
constraint `UNIQUE_COL_1` unique (`COL_1`)
)