2013-10-06 116 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