簡體   English   中英

Android 導航查看項目僅點擊一次

[英]Android Navigation View Item clicks only once

我在 android 導航視圖項目單擊中遇到了一個嚴重的奇怪錯誤,當您打開應用程序並停止單擊后,這些項目僅單擊一次,但最奇怪的部分是該項目每次都響應單擊邊緣。

下面是我的主要 xml 代碼

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutDirection="ltr"
    tools:openDrawer="start">

    <include layout="@layout/layout_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:theme="@style/NavigationView"
        app:itemTextColor="@color/black"
        app:itemIconTint="@color/black"
        android:background="@color/white"
        app:headerLayout="@layout/nav_header_main"
        android:elevation="5dp"
        app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

導航菜單項

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:checked="true"
            android:id="@+id/home"
            android:icon="@drawable/ic_home"
            android:title="@string/home" />
        <item
            android:id="@+id/latest"
            android:icon="@drawable/ic_latest"
            android:title="@string/latest" />
        <item
            android:id="@+id/category"
            android:icon="@drawable/ic_category"
            android:title="@string/category" />
    </group>

</menu>

布局框架

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/toolbar"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    </com.google.android.material.appbar.AppBarLayout>

    <FrameLayout
        android:layout_marginTop="55dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frame_one"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我的活動代碼

package com.education.books.MySchoolLibrary;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;

public class NewAct extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    public Toolbar toolbar;
    private ProgressBar progressBar;
    private DrawerLayout drawer;
    private NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Tool");

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        this.navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        View headerview = navigationView.getHeaderView(0);

        findViewById(R.id.frame_one).setOnClickListener(v -> {
            Toast.makeText(this, "frame ti ya werey!!!", Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();

        drawer.closeDrawers();

        if (id == R.id.home) {
            Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.latest) {
            Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.category) {
            Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
       } else {
            Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
        }

        return true;
    }
}

但是,如果我將整個代碼復制到一個全新的應用程序中,它工作正常,請我不知道我做錯了什么。 謝謝。

單擊項目但未在主項目區域上單擊的邊緣的圖片。

我也面臨同樣的問題。找到了一個解決方案,如下所示

  nav_view.menu.findItem(R.id.menu_share).setOnMenuItemClickListener {
        shareApp()
        drawer_layout.closeDrawer(GravityCompat.START)
        true
    }

    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM