Android-Fragment没有替换



我的应用程序中有一个底部导航,我要做的是在单击底部导航视图按钮时加载不同的片段。

但是当我点击另一个按钮时,当前片段不会替换为其他片段。

查看函数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();
}
}

这对我有效:

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) {
loadFragment(new LibraryFragment());
}
//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
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
if(id == R.id.navigation_library) {
loadFragment(new LibraryFragment());
}
else if (id == R.id.navigation_for_you) {
loadFragment(new ForYouFragment());
}
return false;
}
});
}
//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);
}
}
//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();
}
}

最新更新