我有一个CardViews的RecyclerView列表,我正在使用AppCompatActivity。每个CardView都有一个复选框。当我点击复选框时,我想启动一个Contextual Action Bar。我想使用OnCheckedChangeListener并且没有运气。当" chkSelected"正确地显示复选标记。单击复选框,再次单击" chkSelected时,它将变为不可见。我在这里缺少什么?
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);
}
});
}
我还在ItemHolder()中尝试了OnClickListener()而没有运气。代码如下。 onClick()中的toast正确显示,因此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);
}
}
});
答案 0 :(得分:2)
解决方案是在segments
中设置一个方法,用于更新onBindViewHolder()
中OnClickListeners
的项目视图。