无法从一个片段移动到另一个片段
Unable to move from one fragment to another
我正在尝试设置一个 activity
和一个 fragment
以及一个 recyclerview
和一个底部导航菜单。当我点击 'Dashboard' 时,我可以看到进度条并更新来自 Firestore
的数据,但是当我点击测验时,我看不到进度条或新数据。我所看到的只是相同的数据。我认为它不会移动到其他片段。
作为初学者,我不知道 fragments
和 recyclerview
的实际工作原理,我是在在线人员和资源的帮助下完成的。
我不知道问题出在哪里。如果需要,我可以提供更多代码。
注意:我没有收到任何错误。
bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_vector_dashboard"
android:title="Dashboard" />
<item
android:id="@+id/navigation_quizzes"
android:icon="@drawable/ic_vector_quizzes"
android:title="Quizzes" />
</menu>
mobile_navigation.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/mobile_navigation"
app:startDestination="@+id/navigation_dashboard">
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.quiz.ui.fragments.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
<fragment
android:id="@+id/navigation_quiz"
android:name="com.example.quiz.ui.fragments.QuizFragment"
android:label="@string/title_products"
tools:layout="@layout/fragment_quiz" />
</navigation>
activity_dashboard.xml
<?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"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="@drawable/app_gradient_color_background"
app:itemIconTint="@color/colorWhite"
app:itemTextColor="@color/colorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<androidx.fragment.app.FragmentContainerView
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_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_dashboard.xml
<?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"
android:background="@color/white"
tools:context=".ui.fragments.DashboardFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_dashboard_items"
tools:listitem="@layout/item_list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_no_dashboard_items_found"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="no_dashboard_item_found"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_quiz.xml
<?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"
android:background="@color/white"
tools:context=".ui.fragments.QuizFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvQuizzesFrag"
tools:listitem="@layout/item_list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_no_products_found"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="NO QUIZZES TODAY"
android:textAlignment="center"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
DashboardActivity.kt
package com.example.quiz.ui.activities
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.example.quiz.R
class DashboardActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)
supportActionBar!!.setBackgroundDrawable(
ContextCompat.getDrawable(
this@DashboardActivity,
R.drawable.app_gradient_color_background
)
)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_quiz,
R.id.navigation_dashboard
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
override fun onBackPressed() {
doubleBackToExit()
}
}
你能告诉我这些 R.id.navigation_quiz
、R.id.navigation_dashboard
应该指的是什么地方吗?无论是 bottom_nav_menu.xml
还是 mobile_navigation.xml
.
如果您认为它没有移动到 Quiz Fragment
那么可能是因为您的 BottomNavigation
没有正常工作....
要使底部导航与 Nav Controller 一起正常工作,bottom_nav_menu.xml
和 moile_navigation.xml
中的 id's
项必须相同
在你的情况下,仪表板的 id
在 bottom_nav_menu.xml
和 moile_navigation.xml
中是相同的,但对于测验,这两个文件中的 id 是不同的,它是 @+id/navigation_quizzes
在 bottom_nav_menu.xml
和 moile_navigation.xml
中是 @+id/navigation_quiz
使两个文件中的id相同
bottom_nav_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_vector_dashboard"
android:title="Dashboard" />
<item
android:id="@+id/navigation_quiz"
android:icon="@drawable/ic_vector_quiz"
android:title="Quizzes" />
</menu>
moile_navigation.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/mobile_navigation"
app:startDestination="@+id/navigation_dashboard">
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.quiz.ui.fragments.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
<fragment
android:id="@+id/navigation_quiz"
android:name="com.example.quiz.ui.fragments.QuizFragment"
android:label="@string/title_products"
tools:layout="@layout/fragment_quiz" />
</navigation>
我正在尝试设置一个 activity
和一个 fragment
以及一个 recyclerview
和一个底部导航菜单。当我点击 'Dashboard' 时,我可以看到进度条并更新来自 Firestore
的数据,但是当我点击测验时,我看不到进度条或新数据。我所看到的只是相同的数据。我认为它不会移动到其他片段。
作为初学者,我不知道 fragments
和 recyclerview
的实际工作原理,我是在在线人员和资源的帮助下完成的。
我不知道问题出在哪里。如果需要,我可以提供更多代码。
注意:我没有收到任何错误。
bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_vector_dashboard"
android:title="Dashboard" />
<item
android:id="@+id/navigation_quizzes"
android:icon="@drawable/ic_vector_quizzes"
android:title="Quizzes" />
</menu>
mobile_navigation.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/mobile_navigation"
app:startDestination="@+id/navigation_dashboard">
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.quiz.ui.fragments.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
<fragment
android:id="@+id/navigation_quiz"
android:name="com.example.quiz.ui.fragments.QuizFragment"
android:label="@string/title_products"
tools:layout="@layout/fragment_quiz" />
</navigation>
activity_dashboard.xml
<?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"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="@drawable/app_gradient_color_background"
app:itemIconTint="@color/colorWhite"
app:itemTextColor="@color/colorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<androidx.fragment.app.FragmentContainerView
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_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_dashboard.xml
<?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"
android:background="@color/white"
tools:context=".ui.fragments.DashboardFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_dashboard_items"
tools:listitem="@layout/item_list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_no_dashboard_items_found"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="no_dashboard_item_found"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_quiz.xml
<?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"
android:background="@color/white"
tools:context=".ui.fragments.QuizFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvQuizzesFrag"
tools:listitem="@layout/item_list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_no_products_found"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="NO QUIZZES TODAY"
android:textAlignment="center"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
DashboardActivity.kt
package com.example.quiz.ui.activities
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.example.quiz.R
class DashboardActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)
supportActionBar!!.setBackgroundDrawable(
ContextCompat.getDrawable(
this@DashboardActivity,
R.drawable.app_gradient_color_background
)
)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_quiz,
R.id.navigation_dashboard
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
override fun onBackPressed() {
doubleBackToExit()
}
}
你能告诉我这些 R.id.navigation_quiz
、R.id.navigation_dashboard
应该指的是什么地方吗?无论是 bottom_nav_menu.xml
还是 mobile_navigation.xml
.
如果您认为它没有移动到 Quiz Fragment
那么可能是因为您的 BottomNavigation
没有正常工作....
要使底部导航与 Nav Controller 一起正常工作,bottom_nav_menu.xml
和 moile_navigation.xml
中的 id's
项必须相同
在你的情况下,仪表板的 id
在 bottom_nav_menu.xml
和 moile_navigation.xml
中是相同的,但对于测验,这两个文件中的 id 是不同的,它是 @+id/navigation_quizzes
在 bottom_nav_menu.xml
和 moile_navigation.xml
中是 @+id/navigation_quiz
使两个文件中的id相同
bottom_nav_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_vector_dashboard"
android:title="Dashboard" />
<item
android:id="@+id/navigation_quiz"
android:icon="@drawable/ic_vector_quiz"
android:title="Quizzes" />
</menu>
moile_navigation.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/mobile_navigation"
app:startDestination="@+id/navigation_dashboard">
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.quiz.ui.fragments.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
<fragment
android:id="@+id/navigation_quiz"
android:name="com.example.quiz.ui.fragments.QuizFragment"
android:label="@string/title_products"
tools:layout="@layout/fragment_quiz" />
</navigation>