当前位置: 首页 > 知识库问答 >
问题:

Android导航抽屉项目无响应

窦国源
2023-03-14

抽屉打开正常并正确显示,但当我单击列表中的项目时什么都没有发生。这是我从谷歌教程(UDACITY)中获取的代码。

<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>
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 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" />

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

<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" />

共有1个答案

麹繁
2023-03-14

您应该为navdrawer\u菜单中的项目使用相同的id。导航中的xml和目标。xml。e、 g.在导航中使用aboutFragment id时,应在navdrawer_菜单中使用aboutFragment而不是aboutFragment(使用大写F)。xml

 类似资料:
  • 有人能告诉我如何创建活动到这个主要活动,导航抽屉将看到在所有他们?我需要使用这个特定的MainActivity代码。我不需要使用碎片,只要3个简单的活动将添加到这个抽屉。 NavDrawer布局:

  • 在我的应用程序中,我创建了一个导航抽屉,其中有5个项目,一旦在导航抽屉中单击一个项目,我就可以正确地检查该项目,并使用片段管理器显示新片段。但目前我有三个问题: > 我在某种程度上解决了这个问题,但使用了一个方法,在MainActivity中执行片段事务,同时检查新项。因此,每当一个片段想要调用另一个片段时,它都会使用这个函数传递nav\u drawer\u item\u id作为参数。我认为这个

  • 有什么方法可以确保抽屉菜单保持在片段内容的顶部吗? 我用虚拟数据创建了一个小测试应用程序。10个片段,带有相应的编号按钮和文本视图。问题在于片段元素似乎比导航抽屉具有更高的优先级。 如屏幕截图所示,一旦我试图打开“0片段”,它就会选择注册点击导航抽屉后面的按钮。按下任何其他内容项都可以,但只要它们下面没有其他可交互的项。我该怎么做才能让导航抽屉正确地位于其后面内容的顶部?

  • 我正在使用可在此处下载的导航抽屉示例。 我想扩展这个例子来禁用对其中一个行星的选择。为了简单起见,假设我想永久禁用土星的选择,并将土星的文本更改为深灰色,并且在用户选择它时不突出显示。(实际上,当用户更改屏幕上尚未保存到设备的某些值时,我想以编程方式禁用导航)。 我最接近这一点的是阻止从点击监听器中调用selectItem()方法,但即使我这样做,问题仍然存在——当用户点击时,“Saturn”的文

  • 我是android新手。我有一个关于Android导航抽屉的问题。我在我的应用程序中加入了导航抽屉,一切都很顺利,但我想知道是否有人能帮我在导航抽屉列表中获得惰性动画。 图像的来源。 非常感谢。

  • 我正在将新的Jetpack Android导航与抽屉布局结合使用。当在抽屉XML中使用相同的ID并结合导航图中的片段时,一切都按预期工作。我设置了所有内容: 我现在还想触发自定义操作/代码,而不是在单击抽屉菜单中的项目时执行片段事务。我有一个菜单,单击“注销”时想注销用户: