gpt4 book ai didi

java - 项目上的抽屉导航点击仍保留在主页 fragment 上

转载 作者:行者123 更新时间:2023-12-01 16:24:33 25 4
gpt4 key购买 nike

我对 Android 比较陌生,我想向我的应用程序添加抽屉导航,因此我添加一个新 Activity 并选择抽屉导航 Activity

Activity 类别

import android.os.Bundle;
import android.view.View;
import android.view.Menu;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class HomeActivity extends AppCompatActivity {

private AppBarConfiguration mAppBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}

@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}

当我运行应用程序并尝试单击另一个项目时,没有任何反应,它仍然保留在主 fragment 上到目前为止我看到的大部分信息都是 3.6.3 版本之前的旧信息而其他人只是工作到目前为止我尝试过的- 卸载并重新安装android studio- 更新到最新的android studio(目前为4)- 创建了其他 fragment - 盯着新的抽屉导航项目运行它..同样的事情

过去几天我一直在尝试这个问题,但似乎没有任何效果

标题

最佳答案

您可以在抽屉导航的项目上实现onClick

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener

并添加此方法来处理点击监听器:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
switch (item.getItemId()) {

case R.id.nav_home: {
//do somthing
break;
}

case R.id.nav_gallery: {
//do somthing
break;
}

case R.id.nav_slideshow: {
//do somthing
break;
}
}
//close navigation drawer
drawer.closeDrawer(GravityCompat.END);
return true;
}

以及其他处理点击的方式:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id=menuItem.getItemId();
//it's possible to do more actions on several items, if there is a large amount of items I prefer switch(){case} instead of if()
if (id==R.id.nav_home){
Toast.makeText(getApplicationContext(), "Home", Toast.LENGTH_SHORT).show();
}

if (id==R.id.nav_gallery){
Toast.makeText(getApplicationContext(), "nav_gallery", Toast.LENGTH_SHORT).show();
}

if (id==R.id.nav_slideshow){
Toast.makeText(getApplicationContext(), "slideshow", Toast.LENGTH_SHORT).show();
}
//This is for maintaining the behavior of the Navigation view
NavigationUI.onNavDestinationSelected(menuItem,navController);
//This is for closing the drawer after acting on it
drawer.closeDrawer(GravityCompat.START);
return true;
}
});

关于java - 项目上的抽屉导航点击仍保留在主页 fragment 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62176567/

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