ximsfei / Android-skin-support

Android-skin-support is an easy dynamic skin framework to use for Android, Only one line of code to integrate it. Android 换肤框架, 极低的学习成本, 极好的用户体验. "一行"代码就可以实现换肤, 你值得拥有!!!
MIT License
6.42k stars 1.08k forks source link

databinding设置adapter怎么支持? #392

Open shejishi opened 4 years ago

shejishi commented 4 years ago

databinding设置了adapter, 比如这种用法:

<Button
                    android:id="@+id/setting_mode_content"
                    drawable_isCommonTouch="@{true}"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginTop="@dimen/dp_1"
                    android:gravity="center"
                    android:onClick="onClick"
                    android:paddingLeft="20dp"
                    android:paddingRight="20dp" />

生成的xml,databinding文件:

package com.yilahuo.driftbottle.databinding;
import com.yilahuo.driftbottle.R;
import com.yilahuo.driftbottle.BR;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.View;
@SuppressWarnings("unchecked")
public class ActivitySettingBindingImpl extends ActivitySettingBinding  {

    ...

    @Override
    protected void executeBindings() {
        long dirtyFlags = 0;
        synchronized(this) {
            dirtyFlags = mDirtyFlags;
            mDirtyFlags = 0;
        }
        // batch finished
        if ((dirtyFlags & 0x1L) != 0) {
            // api target 1

            databinding.ViewDrawableDatabinding.setCommonViewTouchBackground(this.rlBind, true);
            databinding.ViewDrawableDatabinding.setCommonViewTouchBackground(this.rlHmd, true);
            databinding.ViewDrawableDatabinding.setCommonViewTouchBackground(this.rlJcgx, true);
            databinding.ViewDrawableDatabinding.setCommonViewTouchBackground(this.rlQingli, true);
            databinding.ViewDrawableDatabinding.setCommonViewTouchBackground(this.rlTuijianPlp, true);
            databinding.ViewDrawableDatabinding.setCommonViewTouchBackground(this.ryDisturb, true);
            databinding.ViewDrawableDatabinding.setCommonViewTouchBackground(this.ryFeedBack, true);
            databinding.ViewDrawableDatabinding.setCommonViewTouchBackground(this.ryNotice, true);
            databinding.ViewDrawableDatabinding.setCommonViewTouchBackground(this.ryTeenagers, true);
            databinding.ViewDrawableDatabinding.setCommonViewTouchBackground(this.settingModeContent, true);
        }
    }
    ...
}

// 使用adapter的文件:

    /**
     * @param view 需要设置的view按钮drawable
     */
    @JvmStatic
    @BindingAdapter(value = ["drawable_isCommonTouch"], requireAll = false)
    fun setCommonViewTouchBackground(view: View, isCommonTouchBtn: Boolean) {
        if (isCommonTouchBtn) {
            setViewBackground(view,
                    GradientDrawable.Orientation.LEFT_RIGHT,
                    ContextCompat.getColor(view.context, R.color.gBackgroundColor),
                    ContextCompat.getColor(view.context, R.color.gBackgroundColor),
                    Color.TRANSPARENT, 0f,
                    ContextCompat.getColor(view.context, R.color.gBlack3Press),
                    0,
                    0,
                    Color.TRANSPARENT.toFloat(),
                    Color.parseColor("#DDDDDD"),
                    0,
                    0,
                    0f,
                    1f, 0f, 0f, 0f, 0f,
                    true
            )
        }
    }

调试发现是没有走 executeBindings 方法,这种能实现吗?

shejishi commented 4 years ago

暂时解决方法是将所有adapter中的属性放入到自定义属性中,然后自己实现一套DatabindingView来适配