我正在尝试为 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/