java - Android - 片段未替换
问题描述
我的应用程序中有一个底部导航,我想做的是在单击底部导航视图按钮时加载不同的片段。
但是当我单击另一个按钮时,当前片段不会替换为另一个片段。
查看函数 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();
}
}
推荐阅读
- filepond - 在 ondata 事件中获取文件路径
- python - Visual Studio Code Python 中的 Pylint 错误
- linux - 如何根据 linux 中的条件(使用 awk 或任何其他)从另一个 csv 文件替换一个 csv 文件的行?
- php - 将数据从三个表单发送到控制器
- encryption - 从未加密的 Redshift 集群迁移到加密的集群
- ruby - 无法使用 DynamoDB GSI
- vb.net - Vb.net 使用 OdbcConnection 登录
- c# - 反序列化 json 中的私有集未正确设置
- java - 线程 awt-eventqueue-2 java.lang.ArithmeticException 中的异常/由零
- syntax - 两种理论之间的观点,包括 MMT 中的共同理论