gpt4 book ai didi

Android MultiChoiceModeListener 支持 2.3

转载 作者:行者123 更新时间:2023-11-30 03:08:34 27 4
gpt4 key购买 nike

为了支持操作栏,我使用 Android 支持库包 V4 和 V7。我在我的应用程序中使用 MultiChoiceModeListener。我如何为 2.3 设备使用 MultiChoiceModeListener..

最佳答案

工作示例 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;
}
};
}

关于Android MultiChoiceModeListener 支持 2.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21364687/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com