Long click on ListFragment

前端 未结 3 759
醉梦人生
醉梦人生 2021-02-05 01:44

I\'m working with a ListFragment and doing a onListItemClick. Everything works fine, but now I want to use a long Item Click (e.g setOnItemLongClickListener(new OnItemLongClickL

3条回答
  •  Happy的楠姐
    2021-02-05 02:13

    Depending on what you want to realize you can use the given methods for context menues:

    First register the View class which gets long pressed (inside your Fragment class):

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        registerForContextMenu(this.getListView());
    }
    

    Than implement these two methods, to create a context menu and do what ever you want when a menu item is clicked:

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
    
        MenuInflater inflater = this.getActivity().getMenuInflater();
        inflater.inflate(R.menu.my_context_menu, menu);
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
    
            case R.id.add: // <-- your custom menu item id here
                // do something here
                return true;
    
            default:
                return super.onContextItemSelected(item);
        }
    }
    

提交回复
热议问题