具有复选框行为的自定义列表视图,如 Gmail 应用程序

发布于 2024-12-17 18:09:15 字数 202 浏览 0 评论 0原文

我在这里阅读了很多有关列表视图和复选框的帖子。他们中的很多人都使用 CheckedTextView 或扩展它。我想实现一个带有复选框行为的自定义列表视图,就像在 android 邮件应用程序(Gingerbread、ICS)上一样:只有复选框是可选的,而不是整行。另外,在 ICS 上,操作栏指示已检查列表项的数量。

任何人都可以向我展示一些代码或指出我正确的方向吗?谢谢!

I have read a lot of threads here about listviews and checkboxes. Lots of them use a CheckedTextView or extend it. I want to implement a custom listview with a checkbox behaviour like on the android mail apps (Gingerbread, ICS): There only checkboxes are checkable and not the whole row. Plus on ICS the actionbar indicates the number of checked list items.

Can anyone please show me some code or point me in the right direction? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫轩蝶泪 2024-12-24 18:09:15

查看 API 演示列表 16 多选模式中的示例

public class List16 extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ListView lv = getListView();
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    lv.setMultiChoiceModeListener(new ModeCallback());
    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_activated_1, 
            Cheeses.sCheeseStrings));
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    getActionBar().setSubtitle("Long press to start selection");
}

private class ModeCallback implements ListView.MultiChoiceModeListener {

    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.list_select_menu, menu);
        mode.setTitle("Select Items");
        return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return true;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
        case R.id.share:
            Toast.makeText(List16.this, "Shared " + getListView().
            getCheckedItemCount() +
                    " items", Toast.LENGTH_SHORT).show();
            mode.finish();
            break;
        default:
            Toast.makeText(List16.this, "Clicked " + item.getTitle(),
                    Toast.LENGTH_SHORT).show();
            break;
        }
        return true;
    }

    public void onDestroyActionMode(ActionMode mode) {
    }

    public void onItemCheckedStateChanged(ActionMode mode,
            int position, long id, boolean checked) {
        final int checkedCount = getListView().getCheckedItemCount();
        switch (checkedCount) {
            case 0:
                mode.setSubtitle(null);
                break;
            case 1:
                mode.setSubtitle("One item selected");
                break;
            default:
                mode.setSubtitle("" + checkedCount + " items selected");
                break;
        }
    }

}
}

Checkout out the sample in API Demos List 16 Multi selection mode

public class List16 extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ListView lv = getListView();
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    lv.setMultiChoiceModeListener(new ModeCallback());
    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_activated_1, 
            Cheeses.sCheeseStrings));
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    getActionBar().setSubtitle("Long press to start selection");
}

private class ModeCallback implements ListView.MultiChoiceModeListener {

    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.list_select_menu, menu);
        mode.setTitle("Select Items");
        return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return true;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
        case R.id.share:
            Toast.makeText(List16.this, "Shared " + getListView().
            getCheckedItemCount() +
                    " items", Toast.LENGTH_SHORT).show();
            mode.finish();
            break;
        default:
            Toast.makeText(List16.this, "Clicked " + item.getTitle(),
                    Toast.LENGTH_SHORT).show();
            break;
        }
        return true;
    }

    public void onDestroyActionMode(ActionMode mode) {
    }

    public void onItemCheckedStateChanged(ActionMode mode,
            int position, long id, boolean checked) {
        final int checkedCount = getListView().getCheckedItemCount();
        switch (checkedCount) {
            case 0:
                mode.setSubtitle(null);
                break;
            case 1:
                mode.setSubtitle("One item selected");
                break;
            default:
                mode.setSubtitle("" + checkedCount + " items selected");
                break;
        }
    }

}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文