android - 使用 android 数据绑定(bind)库绑定(bind)可变参数

标签 android android-databinding

我正在尝试为 SwipeRefreshLayout 编写自定义 setter

setColorScheme(int... 颜色)

但是它的参数好像是varargs。

我现在只能绑定(bind)一种颜色,如下所示:

   @BindingAdapter("app:colorSchemeResources") 
    public static void bindRefreshColor(SwipeRefreshLayout swipeRefreshLayout,  int colorResId) {
            swipeRefreshLayout.setColorSchemeColors(colorResId);
    } 

xml:

  <android.support.v4.widget.SwipeRefreshLayout
                android:id="@+id/swipe_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:colorSchemeResources ="@{@color/primary}"
                />

我的问题是:

如何为可变参数编写自定义 setter?

如何在 xml 文件中绑定(bind)可变参数?

最佳答案

请试试这个:

例如在 colors.xml 中指定一个包含您想要的颜色的整数数组:

    <integer-array name="color_scheme" >
       <item>@color/first_color</item>
        <item>@color/second_color</item>
    </integer-array>

像这样更改您的绑定(bind)适配器:

    @BindingAdapter("app:colorSchemeResources")
    public static void bindRefreshColor(SwipeRefreshLayout swipeRefreshLayout, int[] colorResIds) {
    swipeRefreshLayout.setColorSchemeColors(colorResIds);
    }

并在您的 View 中引用您的数组:

        <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        app:colorSchemeResources ="@{@intArray/color_scheme}"
        android:layout_height="wrap_content">

关于android - 使用 android 数据绑定(bind)库绑定(bind)可变参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36928251/

相关文章:

java - Android ActionBar 问题 - 应用程序崩溃

安卓 SQLite : Replace old database with a new one or use migration scripts

android - 更新android Assets 文件夹中的json文件

Android 数据绑定(bind)默认变量值

android - 具有双向数据绑定(bind)的自定义 View

android - 如何删除按钮背景资源

android - 获取 java.lang.RuntimeException : Failed to call observer method from binding implementation method

android - 如何在 XML 中添加 OR 运算符?

android - gradle 属性为 "experimental and unsupported"意味着什么?

android - 由于意外的 uninstall.exe 文件,更新到 Android Studio 0.8.16 失败