RecyclerView: how to start Contextual Action Bar CAB with a Checkbox?
Asked Answered
T

1

0

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. The checkmark correctly becomes visible when the "chkSelected" Checkbox is clicked on and it becomes invisible when the "chkSelected is clicked on again. 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);
                }
            }); 
}

I also tried an OnClickListener() in the ItemHolder() with no luck. Code is below. The Toast in the onClick() is showing properly so there must be something wrong with the startSupportActionMode().

chkSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(),"Checkbox was clicked",Toast.LENGTH_LONG).show();
                if(mActionMode == null) {
                    // Start the Contextual Action Bar (CAB) using the ActionMode.Callback defined above
                    mActionMode = ((AppCompatActivity) view.getContext()).startSupportActionMode(mActionModeCallback);
                }
            }
        });        
Thallium answered 21/11, 2017 at 6:23 Comment(0)
T
2

Solution was to set up a method in onBindViewHolder() that would update the item views for the OnClickListeners in Itemholder().

Thallium answered 22/11, 2017 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.