0

我的项目包含一个使用 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;
    }
}
4

1 回答 1

0

我自己弄的。

只需将这些添加methodsprivate classMainActivity:

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()如下:CheckboxCustomListViewAdapter

        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());

                boolean checked = false;
                for(int i=0; i < items.size(); i++) {
                    Item temp = items.get(i);

                    if(temp.getChecked() == true) {
                        checked = true;
                    }
                }

                if(checked) {
                    if(MainActivity.actionModeCheck == false) {
                        ((MainActivity) context).showActionMode();
                    }
                }
                else {
                    if(MainActivity.actionModeCheck == true) {
                        ((MainActivity) context).closeActionMode();
                    }
                }
            }
        });

而已 ;)

于 2013-07-23T08:00:06.717 回答