【问题标题】:Android Navigation Drawer item without respondAndroid导航抽屉项目没有响应
【发布时间】:2020-12-06 02:01:52
【问题描述】:

抽屉可以正常打开并正确显示,但是当我单击列表中的项目时没有任何反应。这是我的代码,摘自谷歌教程(UDACITY)。

activity_main.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
            <fragment
                android:id="@+id/myNavHostFragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:defaultNavHost="true"
                app:navGraph="@navigation/navigation"
                />
    </LinearLayout>
    
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/navdrawer_menu"
        app:headerLayout="@layout/nav_header"/>

    </androidx.drawerlayout.widget.DrawerLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

private  lateinit var drawerLayout: DrawerLayout

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    @Suppress("UNUSED_VARIABLE")
    val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
    
    drawerLayout = binding.drawerLayout        
    val navController = this.findNavController(R.id.myNavHostFragment)       
    NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)        
    NavigationUI.setupWithNavController(binding.navView, navController)
}


override fun onSupportNavigateUp(): Boolean {
    val navController = this.findNavController(R.id.myNavHostFragment)        
    return NavigationUI.navigateUp(navController, drawerLayout)

navigation.xml

希望对你有帮助。

<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/navigation"
app:startDestination="@id/titleFragment">

<fragment
    android:id="@+id/titleFragment"
    android:name="com.example.android.navigation.TitleFragment"
    android:label="fragment_title"
    tools:layout="@layout/fragment_title" >
    <action
        android:id="@+id/action_titleFragment_to_gameFragment"
        app:destination="@id/gameFragment" />
</fragment>
<fragment
    android:id="@+id/gameFragment"
    android:name="com.example.android.navigation.GameFragment"
    android:label="fragment_game"
    tools:layout="@layout/fragment_game" >
    <action
        android:id="@+id/action_gameFragment_to_gameOverFragment2"
        app:destination="@id/gameOverFragment2"
        app:popUpTo="@id/gameFragment"
        app:popUpToInclusive="true" />
    <action
        android:id="@+id/action_gameFragment_to_gameWonFragment"
        app:destination="@id/gameWonFragment"
        app:popUpTo="@id/gameFragment"
        app:popUpToInclusive="true" />
</fragment>
<fragment
    android:id="@+id/gameOverFragment2"
    android:name="com.example.android.navigation.GameOverFragment"
    android:label="fragment_game_over"
    tools:layout="@layout/fragment_game_over" >
    <action
        android:id="@+id/action_gameOverFragment_to_gameFragment"
        app:destination="@id/gameFragment"
        app:popUpTo="@id/titleFragment" />
</fragment>
<fragment
    android:id="@+id/gameWonFragment"
    android:name="com.example.android.navigation.GameWonFragment"
    android:label="fragment_game_won"
    tools:layout="@layout/fragment_game_won" >
    <action
        android:id="@+id/action_gameWonFragment_to_gameFragment"
        app:destination="@id/gameFragment"
        app:popUpTo="@id/titleFragment" />
    <argument
        android:name="numQuestions"
        app:argType="integer" />
    <argument
        android:name="numCorrect"
        app:argType="integer" />
</fragment>
<fragment
    android:id="@+id/aboutFragment"
    android:name="com.example.android.navigation.AboutFragment"
    android:label="fragment_about"
    tools:layout="@layout/fragment_about" />
<fragment
    android:id="@+id/rulesFragment"
    android:name="com.example.android.navigation.RulesFragment"
    android:label="fragment_rules"
    tools:layout="@layout/fragment_rules" />

navdrawer_menu.xml

我做的一切都和教程视频一模一样。

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/rulesfragment"
    android:icon="@drawable/rules"
    android:title="@string/rules" />
<item
    android:id="@+id/aboutfragment"
    android:icon="@drawable/about_android_trivia"
    android:title="@string/about" />

【问题讨论】:

  • 你能分享 navigation.xml 和 navdrawer_menu.xml 文件吗?
  • سلام。最重要的事هرچی سرچ کردم چیزی دستگیرم نشد.
  • 好的,我给你写一个答案

标签: android android-studio navigation drawerlayout


【解决方案1】:

您应该为 navdrawer_menu.xml 中的项目和 navigation.xml 中的目的地使用相同的 id。 例如在导航中使用 aboutFragment id 时,应在 navdrawer_menu.xml 中使用 aboutFragment 而不是 aboutfragment(使用大写 F)

【讨论】:

猜你喜欢
  • 2020-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多