如何实现onlongclicklistener到bottom导航菜单项long press?

e5nszbig  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(469)

我在为菜单项创建长按侦听器时遇到问题。
我试过在这方面使用几乎所有的解决方案,但都不起作用。 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;
                }
            };
}
b4lqfgs4

b4lqfgs41#

你可以用 bottomNav.findViewById() 然后传过来 NavView 菜单项id

View.OnLongClickListener listener = new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {

        switch (v.getId()) {
            case R.id.nav_home:
                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;
    }
};

bottomNav.findViewById(R.id.nav_home).setOnLongClickListener(listener);
bottomNav.findViewById(R.id.nav_leaderboard).setOnLongClickListener(listener);
bottomNav.findViewById(R.id.nav_addtree).setOnLongClickListener(listener);
bottomNav.findViewById(R.id.nav_search).setOnLongClickListener(listener);
bottomNav.findViewById(R.id.nav_profile).setOnLongClickListener(listener);

我的测试:

相关问题