onContextItemSelected为片段调用两次

时间:2013-02-06 16:58:52

标签: android android-listview actionbarsherlock android-viewpager contextual-action-bar

我的应用有一个ListView我正在使用Contextual Action Bar用于SDK 11以上的设备以及旧设备的旧弹出式上下文操作。我知道有一种方法可以将CAB与较旧的设备一起使用,但我试图实现它,并发现不值得努力的设备最终会过时。我知道这是一些代码重复,但理论上,我将摆脱旧的弹出行为(强调“在理论上”)。

无论如何,当我使用模拟器时,CAB工作正常,但是当我在该事件中放置断点时,旧设备的旧弹出操作似乎达到了onContextItemSelected两次。我刚刚开始为我的应用实施ViewPager,这在ViewPager之前没有发生,因此不确定是否会导致此问题。

这是我正在使用的代码:

public class MyFragment extends SherlockListFragment
{
    private ListView mListView;
    private android.view.ActionMode mActionMode;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenuInfo menuInfo) 
    {
          super.onCreateContextMenu(menu, v, menuInfo);

         if (this.mActionMode != null) return;

          menu.add(1, 0, 0, "Delete");
          menu.add(1, 1, 0, "Save");
    }

    @Override
    public void onActivityCreated(final Bundle icicle)
    {    
        mListView = getListView();

        if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB)
        {   
            mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

            mListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

                @Override
                public boolean onCreateActionMode(android.view.ActionMode mode, android.view.Menu menu) {
                    // Inflate the menu for the CAB
                    menu.clear();
                    menu.add(1, 1, 2, "Delete").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
                    menu.add(1, 3, 1, "Save").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

                    return true;
                }

                @Override
                public boolean onActionItemClicked(android.view.ActionMode mode, android.view.MenuItem item) {

                    mActionMode = mode;

                    if (item.getGroupId() == 1) 
                    {
                        switch(itemId)
                        {
                            case 0:
                                DeleteItem();
                                break;
                            case 1:
                                SaveItem();
                                break;
                        }
                    }
                }
            }
        }
    }

    @Override
    public boolean onContextItemSelected(final android.view.MenuItem item) {

        if (item.getGroupId() == 1) {

            final AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
            final Integer position = info.position;
            final int itemId = item.getItemId();

            switch(itemId)
            {
                case 0:
                    DeleteItem();
                    break;
                case 1:
                    SaveItem();
                    break;
            }
        }

        return super.onContextItemSelected(item);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {

        super.onPrepareOptionsMenu (menu);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.my_menu, menu);

        super.onCreateOptionsMenu(menu, inflater);
    }
}

ViewPager代码

public class Main extends SherlockFragmentActivity
{
    private static List<Integer> mIds;

    @Override
    public void onCreate(final Bundle icicle)
    {    
        super.onCreate(null);

        setContentView(R.layout.main);

        mViewPager = (ViewPager)findViewById(R.id.viewpager); //view pager exists, so we are using the portait layout

        if (mViewPager != null)
        {
            mIds = new ArrayList<Integer>();

            mIds.add(0);
            mIds.add(1);
            mIds.add(2);
        }
        else //in landscape
        {           
            ListFragment lf = (ListFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentList);

            if (lf == null)
                lf = new ListFragment();

            DetailFragment df = (DetailFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentDetail);

            if (df == null)
            {
                df = new DetailFragment();
                df.setArguments(getIntent().getExtras());   
            }

            getSupportFragmentManager().beginTransaction().add(R.id.fragmentList, lf).commit();
            getSupportFragmentManager().beginTransaction().add(R.id.fragmentDetail, df).commit();
        }
    }       

    private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter  {  

        public MyFragmentPagerAdapter(FragmentManager fm) {  
             super(fm);  
        }  

        @Override  
        public Fragment getItem(int index) {        
            //can't use getSupportFragmentManager().findFragmentById() here because I get a "Cannot make a static reference to the non-static method" error
            if (index == 0)
                return ListFragment.newInstance();
            else            
                return DetailFragment.newInstance(mIds.get(index-1));
        }  

        @Override
        public int getCount() {  
             return 4;
        }
   }  
}

2 个答案:

答案 0 :(得分:3)

这个问题的解决方案解决了我的问题:

How to handle onContextItemSelected in a multi fragment activity?

getUserVisibleHint()中使用onContextItemSelected

答案 1 :(得分:0)

方法必须返回true才能使用选择。 Docs here

    @Override
    public boolean onContextItemSelected(final android.view.MenuItem item) {

        if (item.getGroupId() == 1) {

            final AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
            final Integer position = info.position;
            final int itemId = item.getItemId();

            switch(itemId)
            {
                case 0:
                    DeleteItem();
                    return true;
                case 1:
                    SaveItem();
                    return true;
            }
        }

        return super.onContextItemSelected(item);
    }