【发布时间】:2020-08-10 00:37:48
【问题描述】:
我的应用程序中有一个底部导航,我要做的是在单击底部导航视图按钮时加载不同的片段。
但是当我点击另一个按钮时,当前片段不会被另一个片段替换。
查看函数 OnNavigationItemSelected(在开关盒内)。 我给出了两个片段,但是点击按钮并没有什么区别。
请帮忙!提前谢谢???
这是我的代码:
public class MainActivity extends AppCompatActivity {
//code for the things are populated when the activity is created
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//creating teh tool bar
Toolbar customToolbar = findViewById(R.id.customToolbar);
setSupportActionBar(customToolbar);
//creating the fragments view
//checking for the fragment container
if(findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
//creating the fragment
LibraryFragment libraryFragment = new LibraryFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
libraryFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, libraryFragment).commit();
}
}
//to add menu to the app bar we need to inflate the menu first
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater customMenuInflater = getMenuInflater();
//if the menu does not work try to restart the android studio after clearing the cache its in the File option
customMenuInflater.inflate(R.menu.custom_app_bar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
//actions when the items on the menu are clicked
//it is incomplete because we have'nt created any activities yet
@Override
public boolean onOptionsItemSelected (MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
//open the search bar fragment
return true;
case R.id.action_settings:
//open the settings activity
return true;
case R.id.action_account:
//open the account activity
return true;
default:
//we can't recognise the user action so
//super class will handel it
return super.onOptionsItemSelected(item);
}
}
//Bottom navigation menu actions on the selection of the items
//it will be used to trigger the functions to call the fragments
//to load the fragments in the activity
private BottomNavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment fragment;
switch (menuItem.getItemId()) {
case R.id.navigation_library:
//Library fragment
loadFragment(new LibraryFragment());
return true;
case R.id.navigation_for_you:
//ForYou fragment
loadFragment(new ForYouFragment());
return true;
case R.id.navigation_browse:
//ignore this for the moment
return true;
case R.id.navigation_radio:
//ignore this for the moment
return true;
default:
//ignore this for the moment
return false;
}
}
};
//function to load the fragment into the fragment container
public void loadFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.commit();
}
}
【问题讨论】:
-
我没有找到你在哪里初始化和 setListener for bottomNavigationView?1.initialize bottomNavigationView in onCreate 2.set listener
标签: java android android-studio user-interface android-fragments