我有RecyclerView
,有60个项目,进入RadioGroup的有2个RadioButton
...,我有Fab button
,然后单击它,Snack bar
告诉我你没有权限进入下一页,因为您已经检查了所有60个广播组。我在“小吃店中的操作”设置为“不了解哪个RadioGroup
未选中...并且TextColor
的取消选中radio group
发生了变化。如何解决?
检查是否所有RadioButton检查的方法
//Select All Radio Group
public boolean allSelected() {
boolean allChecked = true;
for (Question question : questionList) {
allChecked = question.getSelectedId() != 0;
}
return allChecked;
}
单击fab按钮
public void onResultClick(View view) {
if (allSelected()) {
startActivity(new Intent(MbtiQuestionActivity.this, ResultActivity.class));
} else {
snackbar =
Snackbar.make(coordinator, R.string.check_all_ques_err, Snackbar.LENGTH_SHORT)
.setAction(R.string.snack_find_unchecked_ques, new View.OnClickListener() {
@Override
public void onClick(View view) {
//here should right the code that uncheck radio group
}
});
ViewCompat.setLayoutDirection(snackbar.getView(), ViewCompat.LAYOUT_DIRECTION_RTL);
snackbar.show();
}
}
问题课
public class Question extends BaseQuestion {
private String questTitle; private String firstQuest; private String secondQuest; private int score = 0; private int firstQuestScore = 2; private int secondQuestScore = 3;
private int selectedId;
public Question(String questTitle, String firstQuest, String secondQuest) {
this.questTitle = questTitle;
this.firstQuest = firstQuest;
this.secondQuest = secondQuest;
}
public Question(){
}
public String getQuestTitle() {
return questTitle; }
public void setQuestTitle(String questTitle) {
this.questTitle = questTitle; }
public String getFirstQuest() {
return firstQuest; }
public void setFirstQuest(String firstQuest) {
this.firstQuest = firstQuest; }
public String getSecondQuest() {
return secondQuest; }
public void setSecondQuest(String secondQuest) {
this.secondQuest = secondQuest; }
public int getScore() {
return score; }
public void setScore(int questNum, int questScore) {
this.score = ((questNum * questScore) + questNum); }
public int getFirstQuestScore() {
return firstQuestScore; }
public void setFirstQuestScore(int firstQuestScore) {
this.firstQuestScore = firstQuestScore; }
public int getSecondQuestScore() {
return secondQuestScore; }
public void setSecondQuestScore(int secondQuestScore) {
this.secondQuestScore = secondQuestScore; }
public void mathScore(int questNum, int questScore) { }
public int getSelectedId() {
return selectedId; }
public void setSelectedId(int selectedId) {
this.selectedId = selectedId; }
}
XML中的Fab
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/check_outline"
app:fabSize="normal"
android:onClick="@{handlers::onResultClick}"
android:clickable="true"
android:focusable="true"/>
我使用了DataBinding ...谢谢。
单击“بررسی”,然后更改TextColor或CardView背景!(不检查RadioGroup!)
日志错误:
Process: mbtitest.kiars.me.mbti, PID: 19757
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
at mbtitest.kiars.me.mbti.databinding.QuesItemRowBinding.executeBindings(QuesItemRowBinding.java:196)
at android.databinding.ViewDataBinding.executeBindingsInternal(ViewDataBinding.java:437)
at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:409)
at android.databinding.ViewDataBinding$7.run(ViewDataBinding.java:194)
at android.databinding.ViewDataBinding$8.doFrame(ViewDataBinding.java:286)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:676)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:859)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
答案 0 :(得分:0)
row.xml
<data>
<variable
name="model"
type="com.innovanathinklabs.sample.data.TestViewModel"/>
</data>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardBackgroundColor="@{question.selected ? @color/colorAccent : @android:color/transparent}"
/>
Model.java
import com.yourpackage.BR;
public class Question extends BaseObservable {
private boolean selected;
@Bindable
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
notifyPropertyChanged(BR.selected);
}
}
显示背景
public void setErrorBackground() {
for (Question question : questionList) {
question.setSelected(question.getSelectedId() != 0);
}
}
Snackbar.make(coordinator, R.string.check_all_ques_err, Snackbar.LENGTH_SHORT)
.setAction(R.string.snack_find_unchecked_ques, new View.OnClickListener() {
@Override
public void onClick(View view) {
setErrorBackground(); //call this
}
});
要删除错误背景,请从任何地方调用此方法
public void removeErrorBackground() {
for (Question question : questionList) {
question.setSelected(false);
}
}
更新
如果要在用户检查后删除背景,请执行此操作
app:cardBackgroundColor="@{model.selected && model.selectedId == 0 ? @color/colorAccent : @android:color/transparent}"
此处&&
表示 &&
更新2
删除所选字段。
public class Question extends BaseObservable {
private boolean showErrorBackground;
@Bindable
public boolean isShowErrorBackground() {
return showErrorBackground;
}
public void setShowErrorBackground(boolean showErrorBackground) {
this.showErrorBackground = showErrorBackground;
notifyPropertyChanged(BR.selected);
}
}
在布局中
app:cardBackgroundColor="@{model.showErrorBackground && model.selectedId == 0 ? @color/colorAccent : @android:color/transparent}"
活动中
private void showHideErrorBackground(boolean show) {
for (Question question : list) {
question.setShowErrorBackground(show);
}
adapter.notifyDataSetChanged();
}
在SnakeBar的onClick中
showHideErrorBackground(true);
更新
public class Question extends BaseObservable {
private boolean showErrorBackground;
private int selectedId;
@Bindable
public boolean isShowErrorBackground() {
return showErrorBackground;
}
public void setShowErrorBackground(boolean showErrorBackground) {
this.showErrorBackground = showErrorBackground;
notifyPropertyChanged(BR.selected);
}
@Bindable
public int getSelectedId() {
return selectedId;
}
public void setSelectedId(int selectedId) {
this.selectedId = selectedId;
notifyPropertyChanged(BR.selectedId);
}
}
我们必须使selectedId
具有可绑定性,因为我们想在更改时调用其用法。