支持操作栏我在我的应用程序中使用Android支持库包V4和V7.Im使用MultiChoiceModeListener。如何为2.3设备使用MultiChoiceModeListener ..
答案 0 :(得分: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;
}
};
}
答案 1 :(得分:0)
我正在成功使用此库,其中包含最少的SDK 7。
https://github.com/akalipetis/action-mode-list-fragment/
ListFragment实现,用于使用AppCompat库处理ActionMode显示和管理,并向后兼容2.1。