[英]How to enable menu item with a click of items of recycler view?
I have a recycler view with checkboxes in InterestsActivity and I want to enable NEXT button on the toolbar after I got at least 4 checkboxes selected. 我有一个Recycler视图,其中带有InterestsActivity中的复选框,并且我至少选择了4个复选框后,想要在工具栏上启用NEXT按钮 。
NEXT button is disabled in onCreateOptionsMenu (Menu menu) 在onCreateOptionsMenu (菜单菜单)中禁用了NEXT按钮
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_interests_button, menu);
nextBtn = menu.findItem(R.id.action_button);
nextBtn.setEnabled(false);
return true;
}
I have overridden onPrepareOptionsMenu (Menu menu) and trying to enable menu item there with a call to invalidateOptionsMenu () in onClick of my ViewHolder item.. 我已覆盖onPrepareOptionsMenu (菜单菜单),并尝试通过在ViewHolder项目的onClick中调用invalidateOptionsMenu ()来启用菜单项。
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem item = menu.findItem(R.id.action_button);
if(interestMap.size()>=4)
item.setEnabled(true);
return true;
}
My bind method in ViewHolder 我在ViewHolder中的 bind方法
public void bind(final Topic subject) {
interestMap=new HashMap<>();
mSubjectCheckbox.setText(subject.getSubjectName());
mSubjectCheckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
interestMap.put(subject.getSubjectName(),true);
if(interestMap.size()>=4)
InterestsActivity.this.invalidateOptionsMenu();
}
});
}
But the button is not being enabled, am I doing right or missing anything? 但是该按钮未启用,我是在做正确的事情还是缺少任何东西?
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_button) {
mFirebaseUtil.mFirestore.collection("users").document(FirebaseUtil.getCurrentUserId()).collection("interests").add(interestMap).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Intent setupIntent = new Intent(InterestsActivity.this, SetupActivity.class);
startActivity(setupIntent);
finish();
}
});
return true;
}
return super.onOptionsItemSelected(item);
}
Please help me resolve this. 请帮我解决这个问题。 Thanks in Advance.
提前致谢。
There seems to be nothing wrong with this code. 这段代码似乎没有错。
You should check if condition interestMap.size()>=4
gets correct value, that is your interestMap
is populated correctly. 您应该检查条件
interestMap.size()>=4
获得正确的值,即您的interestMap
是否正确填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.