2

我在为我的菜单项创建长按侦听器时遇到问题。

我已经尝试在这方面使用几乎所有解决方案,但它没有奏效。 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;
                }
            };
}
4

1 回答 1

0

您可以使用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);

我的测试:

于 2020-12-20T22:14:57.560 回答