我的项目包含一个使用 customlistview 适配器的自定义列表视图。在每一行都有一个复选框 - 如果选中任何复选框,我希望显示一个上下文操作栏(actionMode)。但我完全不知道如何在 CustomListViewAdapter 中实现动作模式。
我阅读的每个教程都是关于将 actionMode 实现到正常的 MainActivity 中,但没有人将它实现到 customlistview 适配器中。这不可能吗?
PS:我正在使用 ActionBarSherlock 库。
其实我想创建一个列表视图,它通常用于创建文件浏览器。如果您勾选项目的复选框,则会出现操作模式并让您选择选项(下载、删除、移动、..)
..:: 这是我的代码::..
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();
}
};
}
项目.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;
}
}