自定义列表视图中包含的Gmail应用程序的复选框行为 [英] custom listview with a checkbox behaviour like on gmail app

查看:165
本文介绍了自定义列表视图中包含的Gmail应用程序的复选框行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了大量的线程在这里对列表视图和复选框。很多人使用CheckedTextView或扩展它。我想实现一个自定义列表视图中包含对Android应用程序的邮件(姜饼,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.

任何人都可以请告诉我一些code或点我在正确的方向?谢谢!

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

推荐答案

结帐在API演示表16多选择模式样本

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;
        }
    }

}
}

这篇关于自定义列表视图中包含的Gmail应用程序的复选框行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆