setNavigationItemSelectedListener 不工作

我的活动不起作用。NavigationViewonClick

以下是我逐个尝试的代码片段,但没有任何效果:

  1. 使用方法实现NavigationView.OnNavigationItemSelectedListenerOnClick()
  2. 设置方法NavigationItemSelectedListener

    nav  = (NavigationView)findViewById(R.id.nav);
    nav.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT).show();
            return true;
        }
    });
    
  3. 使用方法OnOptionItemSelected()

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    if(toggle.onOptionsItemSelected(item))
    {
        return true;
    }
    switch (item.getItemId()) {
        case R.id.lib:
    
            Toast.makeText(getApplicationContext(),"OK",Toast.LENGTH_SHORT).show();
            return true;
    
        case R.id.fav:
            Toast.makeText(getApplicationContext(),"OK",Toast.LENGTH_SHORT).show();
    
            return true;
    }
    return super.onOptionsItemSelected(item);
    }
    

我也尝试了所有这些问题和与我的问题相关的博客,但它不起作用。

  1. 导航抽屉:setNavigationItemSelectedListener 不起作用。
  2. https://stackoverflow.com/questions/47242960/navigationview-setnavigationitemselectedlistener
  3. onNavigationItemSelected 在 NavigationView 中不起作用

这是我的完整主要活动.java文件:

public class MainActivity extends AppCompatActivity{

public frag_song song;
public frag_artist artist;
public  frag_album album;
public TabLayout tab;
Uri songUri;
Cursor songCursor;
public static LinearLayout mainLayout;
int songTitle,songArtist,duration,data,songAlbum;
public NavigationView nav;
public DrawerLayout layout;
public ActionBarDrawerToggle toggle;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME);
    //actionBar.setIcon(R.drawable.logo);
    mainLayout = (LinearLayout)findViewById(R.id.main);

    //All Stuff Of NAvigatin Drawer
    layout = (DrawerLayout)findViewById(R.id.drawerL);
    toggle = new ActionBarDrawerToggle(MainActivity.this,layout,R.string.open,R.string.close);
    layout.addDrawerListener(toggle);
    toggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    nav  = (NavigationView)findViewById(R.id.nav);
    nav.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT).show();
            return true;
        }
    });//Not Working
    nav.setItemIconTintList(null);

    //check if app open first time or not
    SharedPreferences prefs = this.getSharedPreferences("com.blackhat.rhythmbox", Context.MODE_PRIVATE);
    Boolean first = prefs.getBoolean("first", true);
    if(first){
        //Adding song To  Database
        addtoDatabase();
        prefs.edit().putBoolean("first",false).commit();
    }

    //Decaring Object of Fragment
    //TO access Tab
    tab = (TabLayout)findViewById(R.id.tabs);
    song = new frag_song();
    artist = new frag_artist();
    album = new frag_album();


    //addding tab to tab layout
    tab.addTab(tab.newTab().setText("Songs"),true);
    tab.addTab(tab.newTab().setText("ALbum"));
    tab.addTab(tab.newTab().setText("Artist"));

    tab.getTabAt(0).setIcon(R.drawable.icons_song);
    tab.getTabAt(1).setIcon(R.drawable.icons_album);
    tab.getTabAt(2).setIcon(R.drawable.icon_artist);
    //setting default tab
        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().replace(R.id.frame_container,song).commit();

        //on tab select
        tab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition())
                {
                    case 0 :
                        FragmentManager f_song = getSupportFragmentManager();
                        f_song.beginTransaction().replace(R.id.frame_container,song).commit();
                        break;

                    case 1 :
                        FragmentManager f_artist = getSupportFragmentManager();
                        f_artist.beginTransaction().replace(R.id.frame_container,album).commit();
                        break;

                    case 2 :
                        FragmentManager f_album = getSupportFragmentManager();
                        f_album.beginTransaction().replace(R.id.frame_container,artist).commit();
                        break;
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

@Override
protected void onStop() {
    super.onStop();
}

public void addtoDatabase(){
    song_db dbHelper = new song_db(getApplicationContext());
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    ContentResolver contentResolver = getContentResolver();
    songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    songCursor = contentResolver.query(songUri, null, null, null, null);
    //checking if cursor is null or not

    if(songCursor !=null && songCursor.moveToFirst()){
        songTitle = songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
        songArtist = songCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
        duration = songCursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
        songAlbum = songCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
        data = songCursor.getColumnIndex(MediaStore.Audio.Media.DATA);
        Toast.makeText(getApplicationContext(),"Loaded",Toast.LENGTH_SHORT).show();
        do{
            contentValues.put("name",songCursor.getString(songTitle));
            contentValues.put("artist",songCursor.getString(songArtist));
            contentValues.put("album",songCursor.getString(songAlbum));
            contentValues.put("path",songCursor.getString(data));
            contentValues.put("duration",songCursor.getString(duration));
            contentValues.put("isfavorite",0);
            contentValues.put("playlist","regular");
            // contentValues.put("name",songCursor.getString(songArtist));
            db.insert("songs",null,contentValues);
        }while (songCursor.moveToNext());
    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(toggle.onOptionsItemSelected(item))
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}


答案 1

我遇到了同样的问题,我发现Android在布局XML方面非常特别。我有我的NavigationView作为DraverLayout的第一个孩子,但由于一些愚蠢的原因,它必须是最后一个孩子。因此,您的布局必须按以下顺序包含元素:

<DrawerLayout>
    <FrameLayout/>
    <NavigationView/>
</DrawerLayout>

不按以下顺序排列:

<DrawerLayout>
    <NavigationView/>
    <FrameLayout/>
</DrawerLayout>

答案 2

这对我来说很有效,把视野带到了前面

navigationView.setNavigationItemSelectedListener(this);
navigationView.bringToFront();

推荐