Navigation Drawer菜单项不可单击

问题描述 投票:0回答:3

当我从导航抽屉中选择任何选项时,什么都没有发生。一旦我点击导航抽屉,抽屉就会关闭。如果您想从那里进行更正,我也会添加我的Github repo链接。https://github.com/tanmay380/andorid-studio-

这是我的AlphabetBaseShow类,它显示导航抽屉(我将删除一些行,以便使用更少的空间)

package com.example.myapplication.background;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Environment;
import android.speech.tts.TextToSpeech;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
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.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.infront.CharacterSelection;
import com.example.myapplication.infront.SplashScreen;
import com.software.shell.fab.ActionButton;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Locale;

public class alphabetBaseShow extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    protected String mPracticeString;
    protected DrawingView mDrawView;
    public SeekBar seekBar;
    Button button;
    public boolean draw = true;
    AudioManager audio;
    protected TextView mScoreTimerView;
    protected TextView mBestScoreView;
    protected boolean mDone;
    public AlertDialog.Builder alertDialog;
    private static final int STORAGE_PERMISSION_CODE = 101;
    public TextView colorset;
    public NavigationView navigationView;
    //private TextToSpeech textToSpeech;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            System.gc();
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_alphabet_show);

            Toolbar toolbar = findViewById(R.id.PracticeToolbar);
            setSupportActionBar(toolbar);
            // textToSpeech=new TextToSpeech(this,LanguageSelection);
            drawerLayout = findViewById(R.id.drawerLayout);
            actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.close, R.string.open);
            drawerLayout.addDrawerListener(actionBarDrawerToggle);
            actionBarDrawerToggle.syncState();

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);


            mDrawView = new DrawingView(this);
            seekBar = findViewById(R.id.size1);
            mDrawView = findViewById(R.id.DrawingView);
            mBestScoreView = (TextView) findViewById(R.id.best_score_View);
            mScoreTimerView = (TextView) findViewById(R.id.score_and_timer_View);
            mPracticeString = getIntent().getStringExtra(getResources().getString(R.string.practice_string));
            mDone = false;
            mScoreTimerView.bringToFront();

            mDrawView.canVibrate(true);
            mDrawView.setBitmapFromText(mPracticeString);

            audio = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);

        /*switch (audio.getStreamVolume(AudioManager.STREAM_MUSIC)) {
            case AudioManager.RINGER_MODE_SILENT:
                Toast.makeText(getApplicationContext(), "TURN UP THE VOLUME", Toast.LENGTH_SHORT).show();
        }*/
            if ((audio.getStreamVolume(AudioManager.STREAM_MUSIC) > 1)) {
                SplashScreen.TTSobj.speak(mPracticeString, TextToSpeech.QUEUE_FLUSH, null, null);
            } else if (audio.getStreamVolume(AudioManager.STREAM_MUSIC) == 0) {
                audio.setStreamVolume(AudioManager.STREAM_MUSIC, 10, 1);

                SplashScreen.TTSobj.speak(mPracticeString, TextToSpeech.QUEUE_FLUSH, null, null);
            }
        } catch (Exception e) {
            showErrorDialog(e);
        }

    }


    private void toggleSeekbar() {
        if (seekBar.getVisibility() == View.INVISIBLE) {
            seekBar.setVisibility(View.VISIBLE);
            seekBar.bringToFront();
            mDrawView.candraw(false);
        } else {
            seekBar.setVisibility(View.INVISIBLE);
            mDrawView.candraw(true);
        }

    }

    public void clickit() {
        toggleSeekbar();
        setSize();
    }


    public void setSize() {
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mDrawView.setCurrentWidth(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                toggleSeekbar();
            }
        });
        return;
    }

    public void practiceOnClick(View v) {
        switch (v.getId()) {
            case R.id.reset:
                if (mDone) {
                    mDrawView.init();
                }

                mDrawView.setBitmapFromText(mPracticeString);
                mBestScoreView.setAnimation(Animator.createFadeOutAnimation());
                mDrawView.startAnimation(Animator.createScaleUpAnimation());
                mScoreTimerView.setAnimation(Animator.createFadeOutAnimation());

                Animator.createYFlipForwardAnimation(findViewById(R.id.done));
                ((ActionButton) findViewById(R.id.done)).setImageResource(R.drawable.ic_done);
                Animator.createYFlipBackwardAnimation(findViewById(R.id.done));
                mDone = false; //implies that the user isn't done
                mDrawView.candraw(true);

                break;

            case R.id.done:
                String result = mDrawView.saveBitmap(mPracticeString, "");
                if (result.charAt(0) == '/')
                    result = "Trace Saved";

                Toast.makeText(this, result, Toast.LENGTH_SHORT).show();//Toast displayed with the status of saving the trace
                if (!mDone) {
                    float best = SplashScreen.mDbHelper.getScore(mPracticeString);
                    if (best < mDrawView.score()) {
                        best = mDrawView.score();
                        SplashScreen.mDbHelper.writeScore(mPracticeString, best);
                    }

                    //Animations for when the user is done with the trace
                    mDrawView.startAnimation(Animator.createScaleDownAnimation());
                    findViewById(R.id.best_score_View).bringToFront();
                    ((TextView) findViewById(R.id.best_score_View)).setText("Best: " + String.valueOf(best));
                    mScoreTimerView.setText("Score: " + String.valueOf(mDrawView.score()));
                    mScoreTimerView.setAnimation(Animator.createFadeInAnimation());
                    mBestScoreView.setAnimation(Animator.createFadeInAnimation());

                    Animator.createYFlipForwardAnimation(findViewById(R.id.done));
                    ((ActionButton) findViewById(R.id.done)).setImageResource(R.drawable.ic_save);
                    Animator.createYFlipBackwardAnimation(findViewById(R.id.done));

                    mDrawView.candraw(false);
                    mDone = true;

                }
        }
    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        if (!mDone) {
            int next = Arrays.asList(SplashScreen.CHARACTER_LIST).indexOf(mPracticeString);
            switch (item.getItemId()) {
                case R.id.action_next:
                    next = (next + 1) % SplashScreen.CHARACTER_LIST.length;
                    break;
                case R.id.action_previous:
                    next = (next + SplashScreen.CHARACTER_LIST.length - 1) % SplashScreen.CHARACTER_LIST.length;
                    break;
                case R.id.setting:
                    clickit();
                    break;


            }
            if (item.getItemId() == R.id.action_next || item.getItemId() == R.id.action_previous) {
                mPracticeString = SplashScreen.CHARACTER_LIST[next];
                mDrawView.setBitmapFromText(mPracticeString);
                SplashScreen.TTSobj.speak(mPracticeString, TextToSpeech.QUEUE_FLUSH, null, null);
            }
        }
        return super.onOptionsItemSelected(item);
    }   

    protected void showErrorDialog(final Exception e) {
        new AlertDialog.Builder(this)
                .setTitle("ERROR")
                .setMessage(Log.getStackTraceString(e))
                .setPositiveButton("Save to File", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            File mediaStorageDir = new File(Environment.getExternalStorageDirectory() + File.separator + getResources().getString(R.string.app_name));
                            File file = new File(mediaStorageDir.getPath() + File.separator + "error.txt");
                            if (file.exists() || file.createNewFile()) {
                                FileOutputStream fOut = new FileOutputStream(file, true);
                                fOut.write(("\n\n" + new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault()).format(new Date()) + "\n\n").getBytes());
                                fOut.write(Log.getStackTraceString(e).getBytes());
                                fOut.flush();
                                fOut.close();
                            }
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int id = menuItem.getItemId();
        switch (id) {
            case R.id.green:
                SplashScreen.TTSobj.speak(mPracticeString, TextToSpeech.QUEUE_FLUSH, null, null);
                break;
            case R.id.red:
                Toast.makeText(getApplicationContext(), "REd", Toast.LENGTH_SHORT).show();
                break;
            case R.id.black:
                Toast.makeText(getApplicationContext(), "REd", Toast.LENGTH_SHORT).show();
                break;
            case R.id.blue:
                Toast.makeText(getApplicationContext(), "REd", Toast.LENGTH_SHORT).show();
                break;

        }
        return true;
    }
}

这是我的xml文件。(删除了一些Textview行,因为它们并不重要,因此将需要较少的空间)]

    <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".background.alphabetBaseShow">

    <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:layout_marginTop="55dp"
        app:headerLayout="@layout/header"
        app:menu="@menu/colors"
        android:clickable="true">

    </android.support.design.widget.NavigationView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/PracticeToolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/ActionbarBg"
            app:title="PRACTICE " />

        <com.example.myapplication.background.DrawingView
            android:id="@+id/DrawingView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="50dp" />

        <com.software.shell.fab.ActionButton
            android:id="@+id/done"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="0dp"
            android:layout_marginRight="0dp"
            android:layout_marginBottom="0dp"
            android:onClick="practiceOnClick"
            app:button_color="@color/ButtonBg"
            app:image="@drawable/ic_done" />

        <com.software.shell.fab.ActionButton
            android:id="@+id/reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true"
            android:onClick="practiceOnClick"
            app:button_color="@color/ButtonBg"
            app:image="@drawable/ic_reset" />
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

这是我的colors.xml(菜单文件。我什至试图在项目内部添加onClick,但没有成功

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/green"
            android:icon="@drawable/ic_save"
            android:title="GREEN" />
        <item
            android:id="@+id/black"
            android:icon="@drawable/ic_save"
            android:title="BLACK" />
        <item
            android:id="@+id/blue"
            android:icon="@drawable/ic_save"
            android:title="BLUE" />
        <item
            android:id="@+id/red"
            android:icon="@drawable/ic_save"
            android:title="RED"
            android:onClick="toast"/>

    </group>
</menu>

P.S。:-请不要删除此问题。我的最后一个问题已删除,因为它没有太多信息。如果是这种情况,也请给我发邮件:-tanmaysinghal @ 380 @ gmail.com或发表评论否则,我必须等待1周才能重新发布问题。

android android-studio navigation-drawer
3个回答
0
投票

He dhakka j kh cmlocvnmknbghkkkkskkxnxzlajjsbd


0
投票

为您的仓库创建了一个拉取请求。签出。

ps.s。-您应该使用相同版本的支持和设计库-不要使用System.gs(!)-您的导航项Click侦听器不起作用的主要原因是-您未分配接口实现

navigationView.setNavigationItemSelectedListener(this);

代替

navigationView.setNavigationItemSelectedListener(new OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            //some stuff here
        }
    });

已经实现了接口NavigationView.OnNavigationItemSelectedListener


0
投票

我只需要将NAVIGATIONDRAWER部分放在最后

© www.soinside.com 2019 - 2024. All rights reserved.