简体   繁体   English

Android 导航查看项目仅点击一次

[英]Android Navigation View Item clicks only once

i am having a serious weird bug in android navigation view item click, the items are only clicking once when you open the app and stops clicking after, but the weirdest part is that the item is responding to click at the edge every time.我在 android 导航视图项目单击中遇到了一个严重的奇怪错误,当您打开应用程序并停止单击后,这些项目仅单击一次,但最奇怪的部分是该项目每次都响应单击边缘。

Below is my main xml code下面是我的主要 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>

Navigation menu items导航菜单项

<?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>

The layout Frame布局框架

<?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>

My Activity code我的活动代码

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;
    }
}

But if i copy this whole code into an entirely new application, it is working fine, pls i don't know what i am doing wrong.但是,如果我将整个代码复制到一个全新的应用程序中,它工作正常,请我不知道我做错了什么。 Thanks.谢谢。

单击项目但未在主项目区域上单击的边缘的图片。

I am also facing same issue.found a work around solution like below我也面临同样的问题。找到了一个解决方案,如下所示

  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