2013-10-06 206 views
1

我想在longClick()上启用多个视图选择。我应该声明一个动作模式对象并调用startActionMode()吗?另外,如何更改单个项目点击的菜单列表?如图所示,我使用文档作为参考。批处理上下文操作模式

ListView listView = getListView(); 
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 
    listView.setMultiChoiceModeListener(new MultiChoiceModeListener() { 

@Override 
public void onItemCheckedStateChanged(ActionMode mode, int position, 
             long id, boolean checked) { 
    // Here you can do something when items are selected/de-selected, 
    // such as update the title in the CAB 
} 

@Override 
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
    // Respond to clicks on the actions in the CAB 
    switch (item.getItemId()) { 
     case R.id.menu_delete: 
      deleteSelectedItems(); 
      mode.finish(); // Action picked, so close the CAB 
      return true; 
     default: 
      return false; 
    } 
} 

@Override 
public boolean onCreateActionMode(ActionMode mode, Menu menu) { 
    // Inflate the menu for the CAB 
    MenuInflater inflater = mode.getMenuInflater(); 
    inflater.inflate(R.menu.context, menu); 
    return true; 
} 

@Override 
public void onDestroyActionMode(ActionMode mode) { 
    // Here you can make any necessary updates to the activity when 
    // the CAB is removed. By default, selected items are deselected/unchecked. 
} 

@Override 
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 
    // Here you can perform updates to the CAB due to 
    // an invalidate() request 
    return false; 
} 

});

回答

2

要更改单个项目的菜单列表,请单击以下代码。

int count=0; 
@Override 
public void onItemCheckedStateChanged(ActionMode mode, int position, 
            long id, boolean checked) { 
if (checked) { 
      count++; 
     } else { 
      count--; 
     } 
mode.invalidate(); // Add this to Invalidate CAB so that it calls onPrepareActionMode 
} 

现在,修改onPrepareActionMode如下

@Override 
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 
if (selCount == 1){ 
    //show the menu here that you want if only 1 item is selected 
} else { 
    //show the menu here that you want if more than 1 item is selected 
     } 
} 
+0

谢谢,但是这是我怎么称呼startActionMode()? – Meenakshi

+0

mActionMode = new UniversityList()。startActionMode(multichoice)其中mActionMode是一个ActionMode对象,multichoice是一个MultiChoiceModeListener对象? – Meenakshi