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

夙愿已清 提交于 2019-12-02 16:38:30

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/47406498/recyclerview-how-to-start-contextual-action-bar-cab-with-a-checkbox

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