material-components/material-components-android

BottomNavigationView item's long click handling

Open

#1368 aperta il 3 giu 2020

Vedi su GitHub
 (3 commenti) (2 reazioni) (0 assegnatari)Java (3023 fork)batch import
Good First IssueWidget: BottomNavigationbug

Metriche repository

Star
 (15.910 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

I want to handle long click on BottomNavigationView items for making own context menu like its in Instagram when user can hold "accout" icon and choose another profile. I use this code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(listener);
        bottomNavigationView.setSelectedItemId(R.id.nav_messages);

        bottomNavigationView.findViewById(R.id.nav_profile).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(getApplicationContext(), R.string.dark_mode, Toast.LENGTH_SHORT).show();
                return true;
            }
        });

        showFragment(new MessagesFragment());
    }

But it works only when I start the app and holding item. If I start clicking on items and after that try to hold item again it does not work. It seems like listener stops working after user starts communicating with BottomNavigationView. Thank you.

Guida contributor