gpt4 book ai didi

java - Android - Fragment 未替换

转载 作者:行者123 更新时间:2023-12-01 17:09:10 25 4
gpt4 key购买 nike

我的应用程序中有一个底部导航,我想做的是在单击底部导航 View 按钮时加载不同的 fragment 。

但是当我单击另一个按钮时,当前 fragment 不会替换为另一个 fragment 。

查看函数 OnNavigationItemSelected(在开关盒内)。我给出了两个 fragment ,但是单击按钮没有任何区别。

请帮忙!预先感谢😊

这是我的代码:

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();
}
}

关于java - Android - Fragment 未替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61442247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com