2014-01-26 80 views

回答

1

工作實例V7

import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.view.ActionMode; 
import android.util.SparseBooleanArray; 

import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.AdapterView.OnItemClickListener; 

public class MainActivity extends ActionBarActivity { 

    static final String[] COUNTRIES = new String[] { 
     "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", 
     "Angola", "Anguilla", "Canada", "France", "Spain" }; 

    private ListView mListView; 
    private ActionMode mMode; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 




     mMode = null; 
     mListView = (ListView) findViewById(R.id.listView1); 
     mListView.setAdapter(new ArrayAdapter(this, 
       android.R.layout.simple_list_item_multiple_choice, COUNTRIES)); 
     mListView.setItemsCanFocus(false); 
     mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     mListView.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       // TODO Auto-generated method stub 
       SparseBooleanArray checked = mListView.getCheckedItemPositions(); 
        boolean hasCheckedElement = false; 
        for (int i = 0 ; i < checked.size() && ! hasCheckedElement ; i++) { 
         hasCheckedElement = checked.valueAt(i); 
        } 

        if (hasCheckedElement) { 
         if (mMode == null) { 
          mMode = startSupportActionMode(new ModeCallback()); 
         } 
        } else { 
         if (mMode != null) { 
          mMode.finish(); 
         } 
        } 


      } 
     }); 

    } 





    private final class ModeCallback implements ActionMode.Callback { 

     @Override 
     public boolean onCreateActionMode(ActionMode mode, Menu menu) { 
      // Create the menu from the xml file 
      MenuInflater inflater = getMenuInflater(); 
      inflater.inflate(R.menu.activity_main, menu); 
      return true; 
     } 

     @Override 
     public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 
      // Here, you can checked selected items to adapt available actions 
      return false; 
     } 

     @Override 
     public void onDestroyActionMode(ActionMode mode) { 
      // Destroying action mode, let's unselect all items 
      for (int i = 0; i < mListView.getAdapter().getCount(); i++) 
       mListView.setItemChecked(i, false); 

      if (mode == mMode) { 
       mMode = null; 
      } 
     } 

     @Override 
     public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
      long[] selected = mListView.getCheckedItemIds(); 
      if (selected.length > 0) { 
       for (long id: selected) { 
        // Do something with the selected item 
       } 
      } 
      mode.finish(); 
      return true; 
     } 
    }; 
} 
+0

這是否也能處理長時間點擊? –

+0

是的,只需添加監聽器: – Wais

+0

listView.setOnItemLongClickListener(新AdapterView.OnItemLongClickListener(){ 公共布爾onItemLongClick(適配器視圖爲arg0,視圖V, INT指數,長ARG3){// 你想在這裏 不管 return true; } }); – Wais