不调用onNavigationItemSelected()

时间:2019-07-25 20:20:20

标签: java android android-fragments

我正在使用底部导航的应用程序。底部导航按钮可导航片段。我编写了隐藏和显示片段的代码。但似乎听者无法正常工作。我最后看到一个名为Нүүр的窗口,并带有底部导航。我的fragment_container什么都不显示。空白。

这是我的活动代码。

package com.example.demo;

import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.view.MenuItem;
import android.widget.FrameLayout;

public class harnaa extends AppCompatActivity {
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment selectedFragment = null;
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    setTitle("Нүүр");
                    //selectedFragment = new Hmme();
                    displayHmme();
                    break;
                case R.id.navigation_dashboard:
                    setTitle("Анхаарамж");
                    //selectedFragment = new Attention();
                    displayAttention();
                    break;
                case R.id.navigation_notifications:
                    setTitle("Тохиргоо");
                    displaySettings();
                    //selectedFragment = new Settings();
                    break;
            }
          //  getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
           //         selectedFragment).commit();
            return true;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTitle("Нүүр");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_harnaa);
    }
    public void displaySettings() {
        Fragment Settings = new Settings();
        Fragment Hmme = new Hmme();
        Fragment Attention = new Attention();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (Settings.isAdded()) { // if the fragment is already in container
            ft.show(Settings);
        } else { // fragment needs to be added to frame container
            ft.add(R.id.fragment_container, Settings, "C");
        }
        // Hide fragment B
        if (Hmme.isAdded()) { ft.hide(Hmme); }
        // Hide fragment C
        if (Attention.isAdded()) { ft.hide(Attention); }
        // Commit changes
        ft.commit();
    }
    protected void displayHmme() {
        Fragment Settings = new Settings();
        Fragment Hmme = new Hmme();
        Fragment Attention = new Attention();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (Hmme.isAdded()) { // if the fragment is already in container
            ft.show(Hmme);
        } else { // fragment needs to be added to frame container
            ft.add(R.id.fragment_container, Hmme, "A");
        }
        // Hide fragment B
        if (Settings.isAdded()) { ft.hide(Settings); }
        // Hide fragment C
        if (Attention.isAdded()) { ft.hide(Attention); }
        // Commit changes
        ft.commit();
    }
    protected void displayAttention() {
        Fragment Settings = new Settings();
        Fragment Hmme = new Hmme();
        Fragment Attention = new Attention();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (Attention.isAdded()) { // if the fragment is already in container
            ft.show(Attention);
        } else { // fragment needs to be added to frame container
            ft.add(R.id.fragment_container, Attention, "B");
        }
        // Hide fragment B
        if (Hmme.isAdded()) { ft.hide(Hmme); }
        // Hide fragment C
        if (Settings.isAdded()) { ft.hide(Settings); }
        // Commit changes
        ft.commit();
    }


}

1 个答案:

答案 0 :(得分:1)

您创建了OnNavigationItemSelectedListener,但未将其分配给BottomNavigationView。首先使用findViewById()获取导航视图,然后设置其onNavigationItemSelectedListener。

例如,如果您的BottomNavigationView具有bot_nav_view,则可以在onCreate()中编写:

  @Override
  protected void onCreate(Bundle savedInstanceState) {
   setTitle("Нүүр");
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_harnaa);


   BottomNavigationView botNavView = findViewById(R.id.bot_nav_view);
   botNavView.setNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

  }