我想使用自定义适配器绑定我的Spinner。我需要双向绑定。我不确定如何使用自定义适配器?
我的微调器是用xml:
定义的 <Spinner
android:id="@+id/add_event_category_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:prompt="@string/spinner_category_title"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/TextView"/>
自定义适配器:
public class EventCategoryAdapter extends ArrayAdapter<Category>
{
private Context context;
private List<Category> values;
public EventCategoryAdapter (@NonNull Context context, int textViewResourceId, @NonNull List<Category> values)
{
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}
public int getCount()
{
return values.size();
}
public Category getItem(int position)
{
return values.get(position);
}
public long getItemId(int position)
{
return position;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView label = new TextView(context);
label.setText(values.get(position).getName());
return label;
}
@NonNull
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
LayoutInflater vi = LayoutInflater.from(getContext());
convertView = vi.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
CheckedTextView label = (CheckedTextView) convertView.findViewById(android.R.id.text1);
label.setText(values.get(position).getName());
return label;
}
}
因此,正如您所看到的,适配器List集合可以填充项目。 OnClick侦听器选择了我的Category对象,Spinner仅显示Category.Name字段作为标签。
现在我需要将我的微调器绑定数据以避免使用onItemSelected
事件。相反,应该绑定选定的项目并自动填充到我的视图模型。我需要双向绑定,因此更改视图模型也应该触发我的微调器的设置选择。
我知道如何绑定文本视图,标签和回收器视图(用于列表),但我不确定如何为我的微调器绑定适配器。 有什么提示吗?
我的尝试:
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="7dp"
app:layout_constraintTop_toBottomOf="@+id/editText4"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:entries="@{user.categories}"
android:selectedItemPosition="@={user.selectedCategoryPosition}"/>
User
课程:
@InverseBindingMethods({@InverseBindingMethod(type = AppCompatSpinner.class, attribute = "android:selectedItemPosition")})
public class User extends BaseObservable
{
public List<Category> categories;
Category category = null;
@Bindable
public Category getCategory()
{
return category;
}
public void setCategory(Category category)
{
this.category = category;
notifyPropertyChanged(BR.category);
}
Integer selectedCategoryPosition = 0;
@BindingAdapter("selectedItemPositionAttrChanged")
void setSelectedItemPositionListener(AppCompatSpinner view, final InverseBindingListener selectedItemPositionChange)
{
if (selectedItemPositionChange == null)
{
view.setOnItemSelectedListener(null);
}
else
{
view.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
selectedItemPositionChange.onChange();
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
}
@InverseBindingAdapter(attribute = "selectedItemPosition")
Integer getSelectedItemPosition(AppCompatSpinner spinner)
{
return spinner.getSelectedItemPosition();
}
@Bindable
public Integer getSelectedCategoryPosition()
{
return selectedCategoryPosition;
}
public void setSelectedCategoryPosition(Integer selectedCategoryPosition)
{
this.selectedCategoryPosition = selectedCategoryPosition;
notifyPropertyChanged(BR.selectedCategoryPosition);
category = categories.get(selectedCategoryPosition);
}
}
在主要活动中:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User();
activityMainBinding.setUser(user);
现在我收到了错误:
错误:任务&#39;:app:transformJackWithJackForDebug&#39;执行失败。
com.android.jack.ir.JNodeInternalError:java.lang.Exception:java.lang.RuntimeException:failed,请参阅日志以获取详细信息。
@BindingAdapter对无效元素:void setSelectedItemPositionListener(android.support.v7.widget.AppCompatSpinner, android.databinding.InverseBindingListener)
答案 0 :(得分:0)
将此添加到您的代码中
@BindingAdapter("selectedItemPosition")
void setSelectedItemPosition(AppCompatSpinner spinner,int position)
{
if(spinner.getSelectedItemPosition()!=position)
spinner.setSelection(position);
}