Android导航视图单击侦听器不起作用

时间:2019-01-25 19:48:38

标签: android navigationview onitemselectedlistener

我使用向导创建了活动。 我删除了我不感兴趣的内容,例如浮动操作按钮。

activity_main_drawer.xml

中创建了我的自定义项目

该向导创建了 onNavigationItemSelected 函数(请参见下面的代码),我添加了代码,退出了活动并返回登录屏幕。

所以我不知道为什么,我在logcat中看不到任何有趣的东西,所以这是一个逻辑问题。我是新手,导航视图让我非常害怕... 让我知道我是否错过了您需要的任何东西。

package com.skrefi.googleplaycopy;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private FirebaseAuth firebaseAuth;

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

        firebaseAuth = FirebaseAuth.getInstance();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout 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();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }


    //Don't know what this is
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    //Don't know what this is either
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //In case I need it for the admin activity >> the 3 dots pop down thingy <<
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        MakeToast("SOMETHING");

        switch (item.getItemId()){
            case R.id.action_settings:
                MakeToast("Settings button clicked");
                return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        switch(item.getItemId()){
            case R.id.item_signout:
                firebaseAuth.getInstance().signOut();
                MakeToast("Signed out!");
                finish();
                startActivity(new Intent(MainActivity.this, LoginActivity.class));
                break;
        }

        //DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        //drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    public void MakeToast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_LONG).show();
    }
}

编辑:

因此,如果将 可见性设置为 gone ,并且在此布局文件中使用了我想要的布局,这可能会造成混淆,因为我得到的屏幕没有工具栏, 这就是我要的。并且我也想在管理部分保留该属性,以便稍后检查用户是否是我,如果是,我会在抽屉中获得一个名为“管理员活动”的新项目,该项目将可见性设置为 visible < / em>这样我就会进入管理屏幕。

这是您(@)要求的:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!--This is for a potential admin screen-->

    <include
        android:id="@+id/include_toolbar_with_threedots_settings"
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

    <android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is for test"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

</android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:0)

在实际开始活动之前,您正在致电finish(),而在开始登录活动之后,您应致电@SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. switch(item.getItemId()){ case R.id.item_signout: firebaseAuth.getInstance().signOut(); MakeToast("Signed out!"); startActivity(new Intent(MainActivity.this, LoginActivity.class)); finish(); break; } //DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); //drawer.closeDrawer(GravityCompat.START); return true; } 。以下代码将启动“登录”活动并关闭“主要”活动。

//var str = "3; -1; 0; 5";
var str = "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61;62;63;64;65;66;67;68;69;70;71;72;75;76;77;78;79;80;81;82;83;84;85;86;87;88;89;90;91;92;1001;1002;1003;1004;1005;1006;1007;73;";
function getMissingNumber(str){
  var arr = str.split(/\;/);
  var missingNum = [];
  for(var i=Math.min(...arr); i<Math.max(...arr); i++){
     if(arr.indexOf(i.toString()) === -1){
       missingNum.push(i);
       if(i){
       alert(i);return; }
     }
  }
  return missingNum;
}
console.log(getMissingNumber(str));
//alert(getMissingNumber(str));