How to handle onContextItemSelected in a multi fragment activity?

前端 未结 11 2077
天命终不由人
天命终不由人 2020-11-29 17:26

I\'m currently trying to adapt my application to use the \"Compatibility Libraries for Android v4\" to provide the benefits of the usage of fragments even to Android 1.6 use

相关标签:
11条回答
  • 2020-11-29 18:14

    I found an easier solution than the exposed:

    public boolean onContextItemSelected(MenuItem item) {
        ListView yourList = (ListView) (ListView) getView().findViewById(R.id.yourList);
    
        if (!yourList.hasFocus())
            return false;
    
        switch(item.getItemId()) {
            ...
        }
    }
    
    0 讨论(0)
  • 2020-11-29 18:15

    Just change

     @Override
        public boolean onContextItemSelected(MenuItem item) {
        return true;
     }
    

    to

    @Override
        public boolean onContextItemSelected(MenuItem item) {
        return super.onContextItemSelected(item); 
     }
    

    and will work great!!!

    0 讨论(0)
  • 2020-11-29 18:16

    If you are using adapters with listviews in your fragment this might help.

    public boolean onContextItemSelected(final MenuItem item) {
        final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    
        //Check if the context menu call came from the list in this fragment (needed for support for multiple fragments in one screen)
        if (info.targetView.getParent() != getView().findViewById(android.R.id.list))
            return super.onContextItemSelected(item);
    
        //Handle context menu item call
        switch (item.getItemId()) {
            ...
        }
    }
    
    0 讨论(0)
  • 2020-11-29 18:16

    In the method changed return true; to return super.onContextItemSelected(item); in my onContextItemSelected() override and everything started working.

    0 讨论(0)
  • 2020-11-29 18:19

    I found an alternative. It does not change anything on my problem above, but it makes it pointless.

    I have remove the context menu completely from my application. Instead I capture the longclick on a list item and change the visible buttons of the action bar in this moment. From the user point of view this is much more tablet like as a context menu.

    In backward compatible applications the actionbar does not exist. So I've decided to build my own (kind of toolbar on top) for the devices pre Honeycomb.

    If you would like to stay with the context menu, I did not find a better solution as the workaround I've mentioned above.

    0 讨论(0)
提交回复
热议问题