Android 菜单不起作用,谁知道怎么解决?
Android menu is not working, who know how can solve it?
我创建了一个新应用程序并使用了 onNavigationItemSelected 函数,但是当我单击购物车、订购并注销时,它不起作用。我不知道发生了什么。有大佬帮忙看看哪里有问题吗?
这是我的home.java代码
public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private AppBarConfiguration mAppBarConfiguration;
FirebaseDatabase database;
DatabaseReference category;
TextView txtFullName;
RecyclerView recycler_menu;
RecyclerView.LayoutManager layoutManager;
FirebaseRecyclerAdapter<Category, MenuViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Menu");
setSupportActionBar(toolbar);
//Init Firebase
database = FirebaseDatabase.getInstance();
category = database.getReference("Category");
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();
Intent cartIntent = new Intent(Home.this, Cart.class);
startActivity(cartIntent);
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView =(NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//Set Name for user
View headerView = navigationView.getHeaderView(0);
txtFullName = (TextView)headerView.findViewById(R.id.txtFullName);
txtFullName.setText(Common.currentUser.getName());
//Load Menu
recycler_menu = (RecyclerView)findViewById(R.id.recycler_menu);
recycler_menu.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recycler_menu.setLayoutManager(layoutManager);
loadMenu();
}
private void loadMenu() {
adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class,R.layout.menu_itme,MenuViewHolder.class,category) {
@Override
protected void populateViewHolder(MenuViewHolder menuViewHolder, Category category, int i) {
menuViewHolder.txtMenuName.setText(category.getC_Name());
Picasso.with(getBaseContext()).load(category.getImage()).into(menuViewHolder.imageView);
final Category clickItem = category;
menuViewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
//Toast.makeText(Home.this, "" + clickItem.getC_Name(), Toast.LENGTH_SHORT).show();
//Get CategoryID and send to new Activity
Intent foodList = new Intent(Home.this, FoodList.class);
//Because CategoryID is key, so we just get key of this item
foodList.putExtra("CategoryId", adapter.getRef(position).getKey());
startActivity(foodList);
}
});
}
};
recycler_menu.setAdapter(adapter);
}
public void onBackPressed(){
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if(drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@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;
}
public boolean onOptionsItemSelected(MenuItem item){
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.nav_menuhome){
Log.d("TAG", "Menu");
} else if (id == R.id.nav_cart){
Log.d("TAG", "Cart");
Intent cartIntent = new Intent(Home.this, Cart.class);
startActivity(cartIntent);
} else if (id == R.id.nav_orders){
Log.d("TAG", "Order");
Intent orderIntent = new Intent(Home.this, OrderStatus.class);
startActivity(orderIntent);
} else if (id == R.id.nav_log_out){
Log.d("TAG", "Log out");
Intent signIn = new Intent(Home.this, SignIn.class);
signIn.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(signIn);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}
这是我的菜单 > activity_home_drawer.xml 代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_menuhome"
android:icon="@drawable/ic_restaurant_black_24dp"
android:iconTint="@android:color/white"
android:title="Menu" />
<item
android:id="@+id/nav_cart"
android:icon="@drawable/ic_shopping_cart_black_24dp"
android:iconTint="@android:color/white"
android:title="Cart" />
<item
android:id="@+id/nav_orders"
android:icon="@drawable/ic_access_time_black_24dp"
android:iconTint="@android:color/white"
android:title="Orders" />
<item
android:id="@+id/nav_log_out"
android:icon="@drawable/ic_exit_to_app_black_24dp"
android:iconTint="@android:color/white"
android:title="Sign Out" />
</group>
</menu>
这是我的activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/overlayBackground"
tools:openDrawer="start">
<include
layout="@layout/app_bar_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_home"
android:background="@color/overlayBackground"
app:itemTextColor="@android:color/white"
app:itemIconTint="@android:color/white"
app:menu="@menu/activity_home_drawer" />
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/mobile_navigation"
app:defaultNavHost="true" />
</androidx.drawerlayout.widget.DrawerLayout>
谁知道怎么解决?我用过Log.d,但是没有得到任何数据,所以我不知道哪里有问题。
Aaa 这个 adapter.startListening();以上代码
recycler_menu.setAdapter(adapter);
并改变这个
adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class,R.layout.menu_itme,MenuViewHolder.class,category) {
至此
Query query = FirebaseDatabase.getInstance().getReference().child("Category"); FirebaseRecyclerOptions<Category> options = new FirebaseRecyclerOptions.Builder<Category>().setQuery(query, Category.class).build();
和Picasso.with(getBaseContext())
至 Picasso.get()
我创建了一个新应用程序并使用了 onNavigationItemSelected 函数,但是当我单击购物车、订购并注销时,它不起作用。我不知道发生了什么。有大佬帮忙看看哪里有问题吗?
这是我的home.java代码
public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private AppBarConfiguration mAppBarConfiguration;
FirebaseDatabase database;
DatabaseReference category;
TextView txtFullName;
RecyclerView recycler_menu;
RecyclerView.LayoutManager layoutManager;
FirebaseRecyclerAdapter<Category, MenuViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Menu");
setSupportActionBar(toolbar);
//Init Firebase
database = FirebaseDatabase.getInstance();
category = database.getReference("Category");
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();
Intent cartIntent = new Intent(Home.this, Cart.class);
startActivity(cartIntent);
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView =(NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//Set Name for user
View headerView = navigationView.getHeaderView(0);
txtFullName = (TextView)headerView.findViewById(R.id.txtFullName);
txtFullName.setText(Common.currentUser.getName());
//Load Menu
recycler_menu = (RecyclerView)findViewById(R.id.recycler_menu);
recycler_menu.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recycler_menu.setLayoutManager(layoutManager);
loadMenu();
}
private void loadMenu() {
adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class,R.layout.menu_itme,MenuViewHolder.class,category) {
@Override
protected void populateViewHolder(MenuViewHolder menuViewHolder, Category category, int i) {
menuViewHolder.txtMenuName.setText(category.getC_Name());
Picasso.with(getBaseContext()).load(category.getImage()).into(menuViewHolder.imageView);
final Category clickItem = category;
menuViewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
//Toast.makeText(Home.this, "" + clickItem.getC_Name(), Toast.LENGTH_SHORT).show();
//Get CategoryID and send to new Activity
Intent foodList = new Intent(Home.this, FoodList.class);
//Because CategoryID is key, so we just get key of this item
foodList.putExtra("CategoryId", adapter.getRef(position).getKey());
startActivity(foodList);
}
});
}
};
recycler_menu.setAdapter(adapter);
}
public void onBackPressed(){
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if(drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@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;
}
public boolean onOptionsItemSelected(MenuItem item){
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.nav_menuhome){
Log.d("TAG", "Menu");
} else if (id == R.id.nav_cart){
Log.d("TAG", "Cart");
Intent cartIntent = new Intent(Home.this, Cart.class);
startActivity(cartIntent);
} else if (id == R.id.nav_orders){
Log.d("TAG", "Order");
Intent orderIntent = new Intent(Home.this, OrderStatus.class);
startActivity(orderIntent);
} else if (id == R.id.nav_log_out){
Log.d("TAG", "Log out");
Intent signIn = new Intent(Home.this, SignIn.class);
signIn.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(signIn);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}
这是我的菜单 > activity_home_drawer.xml 代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_menuhome"
android:icon="@drawable/ic_restaurant_black_24dp"
android:iconTint="@android:color/white"
android:title="Menu" />
<item
android:id="@+id/nav_cart"
android:icon="@drawable/ic_shopping_cart_black_24dp"
android:iconTint="@android:color/white"
android:title="Cart" />
<item
android:id="@+id/nav_orders"
android:icon="@drawable/ic_access_time_black_24dp"
android:iconTint="@android:color/white"
android:title="Orders" />
<item
android:id="@+id/nav_log_out"
android:icon="@drawable/ic_exit_to_app_black_24dp"
android:iconTint="@android:color/white"
android:title="Sign Out" />
</group>
</menu>
这是我的activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/overlayBackground"
tools:openDrawer="start">
<include
layout="@layout/app_bar_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_home"
android:background="@color/overlayBackground"
app:itemTextColor="@android:color/white"
app:itemIconTint="@android:color/white"
app:menu="@menu/activity_home_drawer" />
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/mobile_navigation"
app:defaultNavHost="true" />
</androidx.drawerlayout.widget.DrawerLayout>
谁知道怎么解决?我用过Log.d,但是没有得到任何数据,所以我不知道哪里有问题。
Aaa 这个 adapter.startListening();以上代码
recycler_menu.setAdapter(adapter);
并改变这个
adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class,R.layout.menu_itme,MenuViewHolder.class,category) {
至此
Query query = FirebaseDatabase.getInstance().getReference().child("Category"); FirebaseRecyclerOptions<Category> options = new FirebaseRecyclerOptions.Builder<Category>().setQuery(query, Category.class).build();
和Picasso.with(getBaseContext())
至 Picasso.get()