lundi 20 novembre 2017

RecyclerView: how to startSupportActionMode() with a Checkbox?

I have a RecyclerView list of CardViews and am using AppCompatActivity. Each CardView has a checkbox. When I click on the checkbox I would like to start a Contextual Action Bar. I would like to use an OnCheckedChangeListener and am having no luck. What am I missing here?

public class MyRecylerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    ...    
    private static ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            return false;
        }
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return false;
        }
        @Override
        public void onDestroyActionMode(ActionMode mode) {
        }
    };
    ...
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_contact_item, parent, false);
        final ItemHolder itemHolder = new ItemHolder(view);

        return itemHolder;
    }

    private static class ItemHolder extends RecyclerView.ViewHolder {

        private CheckBox chkSelected;

        private ItemHolder(View itemView) {
            super(itemView);

            chkSelected = (CheckBox) itemView.findViewById(R.id.chkSelected);

            chkSelected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked)
                      mActionMode = ((AppCompatActivity) buttonView.getContext()).startSupportActionMode(actionModeCallback);
                }
            }); 
}




Aucun commentaire:

Enregistrer un commentaire