我的项目包含一个使用自定义列表视图适配器的自定义列表视图。在每一行都有一个复选框 - 如果勾选任何复选框,我想要显示一个上下文操作栏(actionMode)。但我完全不知道如何将操作模式实现到CustomListViewAdapter。
我读过的每个教程都是关于将actionMode实现到普通的MainActivity中,但没有人将其实现到customlistview适配器中。这不可能吗?
PS:我正在使用ActionBarSherlock库。
实际上我想创建一个listview,它通常用于创建文件浏览器。如果勾选项目的复选框,则会显示操作模式,并允许您选择选项(下载,删除,移动,...)
.. ::这是我的代码:: ..
MainActivity.java
public class MainActivity extends Activity {
private ListView listview;
private Button button;
private List<Item> itemList;
private CustomListViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listview = (ListView) findViewById(R.id.listView1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(listener);
listview.setOnItemClickListener(itemListener);
itemList = new ArrayList<Item>();
for(int i=1; i <= 20; i++) {
Item item = new Item(R.drawable.ic_launcher, "Line " + String.valueOf(i), false);
itemList.add(item);
}
adapter = new CustomListViewAdapter(this, R.layout.row, itemList);
listview.setAdapter(adapter);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View view) {
ArrayList<String> checkedList = new ArrayList<String>();
for(int i=0; i < itemList.size(); i++) {
Item item = adapter.getItem(i);
if(item.getChecked() == true) {
checkedList.add(String.valueOf(item.getFile()));
}
}
String output = "";
for(int i=0; i < checkedList.size(); i++) {
output += checkedList.get(i) + "\n";
}
if(!output.equals("")) {
Toast.makeText(MainActivity.this, output, Toast.LENGTH_SHORT).show();
}
}
};
private OnItemClickListener itemListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Item item = adapter.getItem(position);
Toast.makeText(MainActivity.this, item.getFile(), Toast.LENGTH_SHORT).show();
}
};
}
Item.java
public class Item {
private int imageID;
private String file;
private boolean checked;
public Item() {
}
public Item(int imageID) {
this.imageID = imageID;
}
public Item(int imageID, String file) {
this.imageID = imageID;
this.file = file;
}
public Item(int imageID, String file, boolean checked) {
this.imageID = imageID;
this.file = file;
this.checked = checked;
}
public int getImageID() {
return imageID;
}
public void setImageID(int imageID) {
this.imageID = imageID;
}
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public boolean getChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
}
CustomListViewAdapter.java
public class CustomListViewAdapter extends ArrayAdapter<Item> {
private Context context;
public CustomListViewAdapter(Context context, int resourceID, List<Item> items) {
super(context, resourceID, items);
this.context = context;
}
private class ViewHolder {
ImageView imageview;
TextView textview;
CheckBox checkbox;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Item item = (Item) getItem(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(convertView == null) {
convertView = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.imageview = (ImageView) convertView.findViewById(R.id.imageView1);
holder.textview = (TextView) convertView.findViewById(R.id.textView1);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox checkbox = (CheckBox) view;
Item item = (Item) checkbox.getTag();
item.setChecked(checkbox.isChecked());
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setTag(item);
holder.imageview.setImageResource(item.getImageID());
holder.textview.setText(item.getFile());
holder.checkbox.setChecked(item.getChecked());
return convertView;
}
}
答案 0 :(得分:0)
我自己得到了它。
只需将这些methods
和private class
添加到MainActivity:
public void showActionMode() {
actionMode = startActionMode(new AnActionModeOfEpicProportions());
}
public void closeActionMode() {
actionMode.finish();
}
private final class AnActionModeOfEpicProportions implements ActionMode.Callback {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.add("Download")
.setTitle("Download")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add("Move")
.setTitle("Move")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add("Delete")
.setTitle("Delete")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add("Select all")
.setTitle("Select all")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add("Unselect all")
.setTitle("Unselect all")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
mode.finish();
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
}
..并修改onClickListener()
中Checkbox
的{{1}},如下所示:
CustomListViewAdapter
就是这样;)