我在为我的菜单项创建长按侦听器时遇到问题。
我已经尝试在这方面使用几乎所有解决方案,但它没有奏效。
OnNavigationItemSelected
完美地工作,但从OnLongClickListener
不解雇我所做的任何事情。当我MenuItem
通过它的 ID 声明并找到它时,没有可用的函数可以做某事OnLongClick
。
我究竟做错了什么?
导航.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_home"
android:icon="@drawable/home_navigation_button"
/>
<item
android:id="@+id/nav_leaderboard"
android:icon="@drawable/leaderboard_navigation_button"
/>
<item
android:id="@+id/nav_addtree"
android:icon="@drawable/addtree"
/>
<item
android:id="@+id/nav_search"
android:icon="@drawable/search_navigation_button"
/>
<item
android:id="@+id/nav_profile"
android:icon="@drawable/profile_navigation_button"
/>
</menu>
这是我的活动布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginTest">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/navigation"
/>
</RelativeLayout>
这是我的活动
public class LoginTest extends AppCompatActivity {
TextView userID;
DataPresentersManager dataPresentersManager=new DataPresentersManager();
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_test);
userID=(TextView)findViewById(R.id.userid);
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
userID.setText(user.getUid());
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
bottomNav.setItemIconTintList(null);
//displayMainFragment();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment =null;
switch(item.getItemId()){
case R.id.nav_home:
selectedFragment=dataPresentersManager.currentPresenter.getFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment);
break;
case R.id.nav_leaderboard:
break;
case R.id.nav_addtree:
break;
case R.id.nav_search:
break;
case R.id.nav_profile:
break;
}
return true;
}
};
}