我有这个包含textinput和spinner的表单,表单的内容取决于数据库,所以它以编程方式生成。
我的一些字段,无论是文本输入还是微调器,都是必需的。 因此,如果一些微调器没有选择就会被标记为红色,我现在尝试做的就是在用户选择了某些东西后将它们恢复到原来的颜色。
我对edittext字段没有任何问题,但是当用户更改微调器选择时,微调器不会触发onItemSelected事件。
旋转器的Xml,"项目",然后将这些项目充气并放入网格视图中:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="item"
type="com.test.mymy.ui.models.GrotGridviewItem" />
</data>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="@{item.label}"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="200dp"
android:layout_height="50dp"
android:entries="@{item.values}"
android:selectedItemPosition="@={item.spinnerPosition}"/>
</LinearLayout>
</layout>
在我的活动中:
GroGridviewAdapter champsadapter = new GroGridviewAdapter(activity, grotFields, grotGenerator.getIsChampObligatoire());
GridView champsGridView = binding.formulaire;
champsGridView.setAdapter(champsadapter);
GroGridviewAdapter的getView方法,魔术发生在那里:
@覆盖 public View getView(int position,View view,ViewGroup parent){
final GrotGridviewItem grotGridviewItem = grotGridviewItems.get(position);
int typeNumber = getItemViewType(position);
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
ViewDataBinding binding;
if (typeNumber == GrotGridViewItemTypeEnum.TEXT.getNumber()) {
binding = GrotFieldTextBinding.inflate(layoutInflater, parent, false);
final GrotFieldTextBinding grotFieldTextBinding = (GrotFieldTextBinding) binding;
EditText editText = grotFieldTextBinding.value;
//set it red or transparent when the page first load
if (null != redAtStart.get(grotGridviewItem) && redAtStart.get(grotGridviewItem)) {
editText.setBackgroundColor(GrotColorsEnum.RED.getIntValue());
champIsRed.put(grotGridviewItem.getChampEntity(), true);
}
editText.addTextChangedListener(makeTextWatcher(grotFieldTextBinding, grotGridviewItem));
} else if (typeNumber == GrotGridViewItemTypeEnum.SPINNER.getNumber()) {
binding = GrotFieldSpinnerBinding.inflate(layoutInflater, parent, false);
GrotFieldSpinnerBinding grotFieldSpinnerBinding = (GrotFieldSpinnerBinding) binding;
Spinner spinner = grotFieldSpinnerBinding.spinner;
Log.i("####", "Adding listener to spinner");
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Log.i("##spinner##", String.valueOf(position));
if (spinner.getSelectedItemPosition() != -1) {
spinner.setBackgroundColor(GrotColorsEnum.TRANSPARENT.getIntValue());
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
Log.i("##spinner##", "nothing");
}
});
//set it red or transparent when the page first load
if (spinner.getSelectedItemPosition() != -1) {
spinner.setBackgroundColor(GrotColorsEnum.TRANSPARENT.getIntValue());
champIsRed.put(grotGridviewItem.getChampEntity(), false);
}
if (null != redAtStart.get(grotGridviewItem) && redAtStart.get(grotGridviewItem)) {
spinner.setBackgroundColor(GrotColorsEnum.RED.getIntValue());
champIsRed.put(grotGridviewItem.getChampEntity(), true);
}
} else {
throw new IllegalArgumentException("type N°" + typeNumber + " does not exist");
}
if (null == view) {
view = binding.getRoot();
}
binding.setVariable(BR.item, grotGridviewItem);
return view;
}
我做错了什么? 你需要更多代码吗?