custom listview with a checkbox behaviour like on gmail app

不羁岁月 提交于 2019-12-22 10:03:29

问题


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!


回答1:


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

}
}


来源:https://stackoverflow.com/questions/8224142/custom-listview-with-a-checkbox-behaviour-like-on-gmail-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!