Eva*_*n M 6 android android-fragments kotlin android-jetpack android-architecture-navigation
我正在尝试在现有应用中实现新的 Android 导航组件。我有 2 个相同的片段,除了它们的名字。当我将 startDestination 设置为 fragment2 时,片段似乎显示正确。当 startDestination 设置为 fragment1 时,我看不到膨胀的视图,但我确实看到了“创建的片段 1”吐司。
我做错了什么?
class Fragment1 : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
Toast.makeText(context, "Fragment 1 created", Toast.LENGTH_LONG).show()
return inflater.inflate(R.layout.fragment1, container, false)
}
}
class Fragment2 : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
Toast.makeText(context, "Fragment 2 created", Toast.LENGTH_LONG).show()
return inflater.inflate(R.layout.fragment2, container, false)
}
}
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/fragment1">
<fragment
android:id="@+id/fragment1"
android:name="com.package.anotherpackage.ui.Fragment1"
android:label="fragment1"
tools:layout="@layout/fragment1"/>
<fragment
android:id="@+id/fragment2"
android:name="com.package.anotherpackage.ui.Fragment2"
android:label="fragment2"
tools:layout="@layout/fragment2"/>
</navigation>
Run Code Online (Sandbox Code Playgroud)
主活动布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
android:background="@android:color/background_light"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
片段 1 布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.Fragment1"
>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment 1"
/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
依赖项:
//Navigation
def nav_version = "1.0.0-alpha05"
implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"
// optional - Test helpers
androidTestImplementation "android.arch.navigation:navigation-testing-ktx:$nav_version"
Run Code Online (Sandbox Code Playgroud)
我的项目中的另一个库中有一个名为“fragment1.xml”的布局。它肯定与我试图在 Fragment1 中膨胀的其他布局发生冲突。重命名 Fragment1 布局解决了该问题。
| 归档时间: |
|
| 查看次数: |
8150 次 |
| 最近记录: |