我是Android開發的新手,所以我不懂很多,也許是基本的東西,也許即使是陳述的主題也不能完全反映我的問題,我提前道歉,我無法更準確地表述。請幫我解決一些問題。
我的應用程式中有一個帶有三個選單項的底部導航。就像在圖片上:
底部導航
最初,我按照標準的 Android Studio 示例進行操作:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private NavController navController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
BottomNavigationView navView = findViewById(R.id.nav_view);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_update, R.id.navigation_notifications)
.build();
navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(binding.navView, navController);
}
}
在這段代碼中,選單項切換片段,保存每個片段的狀態(我不知道具體如何,它似乎在導航控制器內部的某個地方)。
但是現在我需要中間按鈕來執行不同的功能,即不參與切換片段。
唯一合適的方法是使用 setOnItemSelectedListener。
然后我的代碼將如下所示:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private NavController navController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
BottomNavigationView navView = findViewById(R.id.nav_view);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_update, R.id.navigation_notifications)
.build();
navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(binding.navView, navController);
navView.setOnItemSelectedListener(
new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
navController.navigate(R.id.navigation_home);
break;
case R.id.navigation_update:
// work for middle menu item
break;
case R.id.navigation_notifications:
navController.navigate(R.id.navigation_notifications, savedInstanceState);
break;
}
return true;
}
});
}
}
它甚至可以作業,只是片段不再保存它們的狀態。也就是比如在第一個fragment中我有一個RecyclerView,如果我向下滾動,切換到另一個fragment,然后回傳,那么RecyclerView處于默認狀態(不滾動),也就是狀態沒有被保存。
事實證明,在代碼的第一個版本中,我不能對中間選單項進行單獨的作業,而在第二個版本中,片段的狀態沒有保存。
提示正確的方法來處理這個問題。這樣可以為中間的選單項分配一個單獨的作業,并且可以保存片段的狀態。
uj5u.com熱心網友回復:
而不是使用 navView.setOnItemClickListener,您可以使用 navController.addOnDestinationChangedListener,并在覆寫的 onDestinationChanged 方法中,您可以檢查
if(destination.id == R.id.navigation_update) {
//your custom action here
}
uj5u.com熱心網友回復:
我能夠使用 Navigation 使用標準工具解決問題。我剛剛找到了一個選單項并在其中添加了一個 OnClickListener。
BottomNavigationItemView updateBtn = findViewById(R.id.navigation_update);
updateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// todo
}
});
因此,切換片段和單擊中間選單項都可以正常作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/459865.html
上一篇:如何在片段中設定系結?
