Итак, у меня, по сути, есть навигационный ящик, который запускается из элементов "navigation_menu. xml", но в приложении есть заголовок с TextView
, который я пытаюсь изменить в зависимости от того, какой элемент ящик навигации выбран. Я пытался использовать onNavigationItemSelected
, но, похоже, это не работает.
Мне интересно, не могли бы вы помочь мне изменить TextView
для различных выбранных предметов?
MainActivity. java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
NavigationView nvView;
TextView tvMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setNavigationViewListener();
final DrawerLayout drawerLayout = findViewById(R.id.drawerLayout);
findViewById(R.id.imageMenu).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
NavigationView nvView = findViewById(R.id.navigationView);
nvView.setItemIconTintList(null);
NavController navController = Navigation.findNavController(this, R.id.navHostFragment);
NavigationUI.setupWithNavController(nvView, navController);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
tvMain = (TextView) findViewById(R.id.tvMainTitle);
switch (item.getItemId()) {
//I have also tried using the case (R.id.nav_home), (R.id.nav_members), etc for this and it doesn't work
case 0:{
tvMain.setText("Realtas");
break;
}
case 1:{
tvMain.setText("Members Fragment");
break;
}
}
return true;}
private void setNavigationViewListener(){
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
}
}
И вот меню / навигационное меню. xml
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home"
android:title="@string/menu_home" />
<item
android:id="@+id/nav_members"
android:icon="@drawable/ic_members"
android:title="@string/menu_members" />
<item
android:id="@+id/nav_map"
android:icon="@drawable/ic_ireland"
android:title="@string/menu_map" />
<item
android:id="@+id/nav_news"
android:icon="@drawable/ic_news"
android:title="@string/menu_news" />
<item
android:id="@+id/nav_livestream"
android:icon="@drawable/ic_livestream"
android:title="@string/menu_livestream" />
<item
android:id="@+id/nav_election"
android:icon="@drawable/ic_election"
android:title="@string/menu_election" />
<item android:title="@string/menu_misc">
<menu>
<item
android:id="@+id/nav_login"
android:icon="@drawable/ic_login"
android:title="@string/menu_login"/>
<item
android:id="@+id/nav_register"
android:icon="@drawable/ic_harp_circle"
android:title="@string/menu_register"/>
</menu>
</item>
<item android:title="Irish Version">
<menu>
<item
android:id="@+id/nav_gaeilge"
android:icon="@drawable/ic_gaeilge"
android:title="@string/menu_gaeilge"/>
</menu>
</item>
</menu>
Также здесь навигация / главное. xml
<navigation 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/main"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.example.fypfinaltrial.HomeFragment"
android:label="Home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_nav_home_to_nav_members"
app:destination="@id/nav_members" />
<action
android:id="@+id/action_nav_home_to_nav_election"
app:destination="@id/nav_election" />
<action
android:id="@+id/action_nav_home_to_nav_login"
app:destination="@id/nav_login" />
<action
android:id="@+id/action_nav_home_to_nav_map"
app:destination="@id/nav_map" />
<action
android:id="@+id/action_nav_home_to_nav_register"
app:destination="@id/nav_register" />
<action
android:id="@+id/action_nav_home_to_nav_livestream"
app:destination="@id/nav_livestream" />
<action
android:id="@+id/action_nav_home_to_nav_news"
app:destination="@id/nav_news" />
</fragment>
<fragment
android:id="@+id/nav_members"
android:name="com.example.fypfinaltrial.MembersFragment"
android:label="Members"
tools:layout="@layout/fragment_members" />
<fragment
android:id="@+id/nav_map"
android:name="com.example.fypfinaltrial.MapFragment"
android:label="Interactive Map"
tools:layout="@layout/fragment_map" >
<action
android:id="@+id/action_nav_map_to_dublinMapFragment"
app:destination="@id/dublinMapFragment"
app:enterAnim="@anim/fragment_open_enter"
app:exitAnim="@anim/fragment_close_exit"/>
</fragment>
<fragment
android:id="@+id/nav_news"
android:name="com.example.fypfinaltrial.NewsFragment"
android:label="Election News"
tools:layout="@layout/fragment_news" />
<fragment
android:id="@+id/nav_election"
android:name="com.example.fypfinaltrial.ElectionFinalFragment"
android:label="Upcoming Election"
tools:layout="@layout/fragment_election" >
<action
android:id="@+id/action_nav_election_to_electionResultsFragment"
app:destination="@id/nav_election_results"
app:enterAnim="@anim/fragment_open_enter"
app:exitAnim="@anim/fragment_close_exit"/>
<action
android:id="@+id/action_nav_election_to_electionFragment"
app:destination="@id/nav_election_map"
app:enterAnim="@anim/fragment_open_enter"
app:exitAnim="@anim/fragment_close_exit"/>
<action
android:id="@+id/action_nav_election_to_nav_check_register"
app:destination="@id/nav_check_register"
app:enterAnim="@anim/fragment_open_enter"
app:exitAnim="@anim/fragment_close_exit"/>
</fragment>
<fragment
android:id="@+id/nav_login"
android:name="com.example.fypfinaltrial.LoginFragment"
android:label="fragment_login"
tools:layout="@layout/fragment_login" />
<fragment
android:id="@+id/nav_register"
android:name="com.example.fypfinaltrial.RegisterFragment"
android:label="Register"
tools:layout="@layout/fragment_register" />
<activity
android:id="@+id/nav_gaeilge"
android:name="com.example.fypfinaltrial.MainActivityGaeilge"
android:label="Home Gaeilge"
tools:layout="@layout/activity_main_gaeilge" />
<fragment
android:id="@+id/dublinMapFragment"
android:name="com.example.fypfinaltrial.DublinMapFragment"
android:label="fragment_dublin_map"
tools:layout="@layout/fragment_dublin_map" />
<fragment
android:id="@+id/livestreamHighlightsFragment"
android:name="com.example.fypfinaltrial.LivestreamHighlightsFragment"
android:label="fragment_livestream_highlights"
tools:layout="@layout/fragment_livestream_highlights" />
<fragment
android:id="@+id/nav_livestream"
android:name="com.example.fypfinaltrial.LivestreamFragment"
android:label="fragment_livestream"
tools:layout="@layout/fragment_livestream" >
<action
android:id="@+id/action_nav_livestream_to_livestreamHighlightsFragment"
app:destination="@id/livestreamHighlightsFragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"/>
</fragment>
<fragment
android:id="@+id/nav_check_register"
android:name="com.example.fypfinaltrial.CheckRegisterFragment"
android:label="fragment_check_register"
tools:layout="@layout/fragment_check_register" />
<fragment
android:id="@+id/nav_election_results"
android:name="com.example.fypfinaltrial.ElectionResultsFragment"
android:label="fragment_election_results"
tools:layout="@layout/fragment_election_results" />
<fragment
android:id="@+id/nav_election_map"
android:name="com.example.fypfinaltrial.ElectionFragment"
android:label="fragment_election"
tools:layout="@layout/fragment_election" />
</navigation>