material-components/material-components-android

BottomNavigationView item's long click handling

開放

#1,368 建立於 2020年6月3日

 (3 則留言) (2 個反應) (0 位負責人)Java (3,023 個分叉)batch import
Good First IssueWidget: BottomNavigationbug

倉庫指標

星標
 (15,910 顆星)
PR 合併指標
 (30 天內沒有已合併 PR)

描述

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.

貢獻者指南