簡體   English   中英

Android數據綁定,如何在drawable中綁定數據?

[英]Android data binding, how to bind data in a drawable?

我有這個用於自定義進度條的 drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape>
        <solid android:color="@color/marm_color_blue_palette_light_primary"></solid>
    </shape>
</item>
<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <solid android:color="@color/marm_color_blue_palette_accent"></solid>
        </shape>
    </clip>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape>
            <solid android:color="@color/marm_color_blue_palette_accent"></solid>
        </shape>
    </clip>
</item>
</layer-list>

如何使用 DataBinding 設置這些顏色? 我是 Android 數據綁定的新手,我來自 Windows Phone MVVM。

我知道,對於活動布局,您必須這樣做:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
    <import type="com.androidadvance.androidsurvey.R" />
    <variable
        name="dynamicUI"  type="com.example.DynamicConfigModel" />
</data>

<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@{dynamicUI.mainBg, default=@color/color_palette_light_divider_text_12}"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@{dynamicUI.darkPrimary , default=@color/marm_color_blue_palette_dark_primary}"
        android:titleTextColor="@{dynamicUI.textLight, default=@android:color/white}"
        />

</LinearLayout>
</layout>

在Java中

ContentLayoutBinding binding = DataBindingUtil.setContentView(this, R.layout.YOURLAYOUT);
        binding.setDynamicUI(YOURMODEL);

如何在可繪制或一般風格中做到這一點?

因為數據綁定基本上是在編譯時生成的代碼,所以它目前僅用於布局。 如果您嘗試在可繪制對象、樣式或菜單中使用它,它將不起作用。

我知道很遺憾架構不完整,但是您會注意到,每當您在布局文件周圍添加布局標記時,它都會生成一個 NameOfActivityBinding 類,在該類中它會為您編寫管理元素更改的所有樣板代碼。 因此,雖然對我們來說它看起來就像 xml 指向一個變量,但 Android 仍在為您編寫所有樣板。

除了我們可以看到它仍然處於早期階段這一事實之外,這與其他框架並沒有太大不同。

您可以將一些屬性更改處理程序創建到自定義控件中,然后利用自定義控件菜單或其他自定義類來替換您分配可繪制對象的類。 然后自定義類可以利用綁定,但這會帶來更多的工作,而不是簡單地更改代碼中的可繪制內容。

我所做的一件事是監視相應項目上的事件,然后像這樣更改可繪制對象的色調。

   @JvmStatic
@BindingAdapter("backgroundDrawableChange")
fun backgroundDrawableChange(view: RelativeLayout, isSelected: Boolean){
    var bgDrawable: LayerDrawable = view.background as LayerDrawable
    var shape: GradientDrawable =  bgDrawable.findDrawableByLayerId(R.id.shapeId) as GradientDrawable
    shape.setColor(Color.parseColor((if (isSelected) YACustomPreference.getInstance(view.context).getPrimaryColorHex() else YACustomPreference.getInstance(view.context).getWhite())))
}

然后我會將其附加到我正在監視更改的屬性上。

      <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:orientation="horizontal"
            tools:visibility="gone"
            android:background="@drawable/rounded_bottom_right_button"
            app:backgroundDrawableChange="@{drawerItemModel.isSelected}"
            android:visibility="@{drawerItemModel.useRightAlignText ? View.VISIBLE : View.GONE}"
            android:gravity="center" />

這是一個簡單的行點擊監視器,具有用戶定義的顏色,顯然不可能在可繪制對象中綁定顏色,所以這是下一個最好的事情。 希望有幫助。

我知道這不完全是你所追求的,但至少可能會讓你走上正確的道路或給你一個想法。

幾天前,我遇到了使用數據綁定設置 drawable 的問題,然后我發現了這種方式,它很簡單,而且工作完美,在 BindingUtils.class 中創建這個方法很簡單

public final class BindingUtils {
    private BindingUtils() {
    }
    @BindingAdapter({"progressDrawable"})
        public static void setProgressDrawable(ProgressBar progressBar, int resource) {
            if (resource != -1)
                progressBar.setProgressDrawable(ContextCompat.getDrawable(progressBar.getContext(),resource));
        }
    }

XML 中的用法:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <import type="android.view.View" />

        <import type="android.text.TextUtils" />

        <variable
            name="viewModel"
            type="com.home.NewViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:alpha="0.80"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center">

        <ProgressBar
            android:id="@+id/progressBar"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:progress="@{viewModel.uploadProgress}"
            progressDrawable="@{viewModel.progressBg}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

視圖模型:

public class NewViewModel extends ViewModel{
    public final static short INIT_PROGRESS = 0;
    public final ObservableInt uploadProgress = new ObservableInt(INIT_PROGRESS);
    public final ObservableInt progressBg = new ObservableInt(R.drawable.bg_post_uploading_style); 
}

暫無
暫無

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

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