0

탐색 항목을 클릭하면 조각을 변경하는 아래쪽 탐색 모음을 구현하려고합니다. 그러나 탐색 모음 항목을 클릭하면 조각이 변경되지 않습니다. log.d을 사용하여 onNavigationItemSelected이 호출되지 않는 것으로 나타났습니다. 어떻게 해결할 수 있습니까? onNavigationItemSelected가 아래쪽 탐색 모음에 대해 호출되지 않음

주의하는 것이

, 그들은 도구 모음의 버튼 일하는 startFeedFragment, startScheduleFragment, & startSettingsFragment가 구현되는 같은 방법으로합니다. 나는 또한 tutorial과이 question을 참조로 도움을 요청했다.

MainActivity.java XML 파일에서

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener { 

protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     setUpRecycler(); 
     startFeedFragment(); 

     BottomNavigationView bottomNavBar = (BottomNavigationView) findViewById(R.id.navigation); 
     bottomNavBar.bringToFront(); 
     bottomNavBar.setOnNavigationItemSelectedListener(this); 
    } 

    @Override 
    public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
     Log.d("NAV BAR", "onNavigationItemSelected hit"); 
     switch (item.getItemId()) { 
      case R.id.feedMenu: 
       startFeedFragment(); 
       Log.d("NAV BAR", "feed"); 
       break; 
      case R.id.myScheduleMenu: 
       startScheduleFragment(); 
       Log.d("NAV BAR", "schedule"); 
       break; 
      case R.id.settingsMenu: 
       startSettingsFragment(); 
       Log.d("NAV BAR", "settings lol"); 
       break; 
      default: 
       Log.d("NAV BAR", "false"); 
       return false; 
     } 
     return true; 
    } 

    /** 
    * Switches out the fragment for {@link FeedFragment} and sets an appropriate title. startScheduleFragmens & startSettingsFragment are implemented the same way 
    */ 
    private void startFeedFragment() 
    { 
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
     transaction.replace(R.id.fragmentContainer, new FeedFragment()); 
     transaction.commit(); 
     getSupportActionBar().setTitle(R.string.title_fragment_feed); 
    } 
} 

니펫

<android.support.constraint.ConstraintLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <FrameLayout 
      android:id="@+id/fragmentContainer" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      app:layout_constraintBottom_toTopOf="@+id/navigation" 
      app:layout_constraintEnd_toEndOf="parent" 
      app:layout_constraintStart_toStartOf="parent" 
      app:layout_constraintTop_toTopOf="parent" /> 

     <android.support.design.widget.BottomNavigationView 
      android:id="@+id/navigation" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:itemBackground="@android:color/white" 
      app:itemIconTint="@color/colorPrimaryDark" 
      app:itemTextColor="@color/colorPrimaryDark" 
      app:layout_constraintBottom_toBottomOf="parent" 
      app:layout_constraintEnd_toEndOf="parent" 
      app:layout_constraintStart_toStartOf="parent" 
      app:menu="@menu/bottom_nav" 
      android:elevation="8dp"/> 
    </android.support.constraint.ConstraintLayout> 

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

enter image description here

답변

0

해결! 메뉴의 버튼을 android:enabled="false"으로 설정했기 때문입니다. 그것은 그들이 누를 수 없음을 의미했기 때문에 onNavigationItemSelected이 호출되지 않았습니다. 이 문제를 해결하기 위해 모든 버튼을 android:enabled="true"으로 설정했습니다. bottom_nav.xml에 대한

올바른 코드

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:id="@+id/bottom_nav_my_schedule" 
     android:title="@string/menu_my_schedule" 
     android:icon="@drawable/ic_event_available_white_24dp" 
     android:enabled="true"/> <!--make sure enabled is set to true!--> 
    <item android:id="@+id/bottom_nav_feed" 
     android:title="@string/menu_feed" 
     android:icon="@drawable/ic_view_list_white_24dp" 
     android:enabled="true" /> <!--make sure enabled is set to true!--> 
    <item android:id="@+id/bottom_nav_settings" 
     android:title="@string/menu_settings" 
     android:icon="@drawable/ic_settings_white_24dp" 
     android:enabled="true"/> <!--make sure enabled is set to true!--> 
</menu> 
관련 문제