簡體   English   中英

Android DataBinding - 使用自定義適配器綁定Spinner

[英]Android DataBinding - bind Spinner with custom adapter

我想使用自定義適配器綁定我的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);

現在我收到錯誤:

錯誤:任務':app:transformJackWithJackForDebug'的執行失敗。

com.android.jack.ir.JNodeInternalError:java.lang.Exception:java.lang.RuntimeException:failure,請參閱日志以獲取詳細信息。
無效元素上的@BindingAdapter:void setSelectedItemPositionListener(android.support.v7.widget.AppCompatSpinner,android.databinding.InverseBindingListener)

將其添加到您的代碼中

@BindingAdapter("selectedItemPosition")
void setSelectedItemPosition(AppCompatSpinner spinner,int position)
{
    if(spinner.getSelectedItemPosition()!=position)
    spinner.setSelection(position);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM