简体   繁体   English

布局表达式中可绘制的三元运算符

[英]ternary operator for drawable in layout expression

This is my fragment_setting.xml . 这是我的fragment_setting.xml

<ToggleButton
    android:layout_width="47dp"
    android:layout_height="27dp"
    android:background="@{settingVm.isNotiOn ? @drawable/btn_on_mid : @drawable/btn_off_mid}"
    android:onClick="@{()->settingVm.changeBtnStatus()}"
    android:text="@string/on"
    android:textOff="on"
    android:textOn="on"
    android:textSize="11sp"
    android:textStyle="bold" />

<ToggleButton
    android:layout_width="47dp"
    android:layout_height="27dp"
    android:background="@{settingVm.isNotiOn ? @drawable/btn_off_mid : @drawable/btn_on_mid}"
    android:onClick="@{()-> settingVm.changeBtnStatus()}"
    android:text="@string/off"
    android:textOff="off"
    android:textOn="off"
    android:textSize="11sp"
    android:textStyle="bold" />

This is my SettingViewModel 这是我的SettingViewModel

class SettingViewModel(handler: SettingHandler) : ViewModel() {
    var handler = handler
    var isNotiOn: Boolean? = true
    var visibility = View.VISIBLE

    init {
        if (BuildConfig.DEBUG) {
            Timber.plant(Timber.DebugTree())
            Timber.d("start")
        }
    }

    fun onBackBtnPressed() {
        Timber.d("onBackBtnPressed()")
        handler.onBackBtnPressed()
    }

    fun showLogoutDialogue() {
        Timber.d("showLogoutDialogue()")
        handler.showLogoutDialogue()
    }

    fun changeBtnStatus(){
        Timber.d("changeBtnStatus()")
        handler.changeBtnStatus()
    }
}

And this is my SettingFragment 这是我的SettingFragment

...
        val spUtil = SharedPreferenceUtil(activity!!)
        when (spUtil.isNotificationOn) {
            false -> {
                binding!!.settingVm!!.isNotiOn = false
            }
            else -> {
                binding!!.settingVm!!.isNotiOn = true
            }
        }
...
    override fun changeBtnStatus() {
        // TODO: Set real notification setting.

        val spUtil = SharedPreferenceUtil(activity!!)
        when (binding!!.settingVm!!.isNotiOn) {
            true -> {
                binding!!.settingVm!!.isNotiOn = false
                spUtil.isNotificationOn = false
            }
            else -> {
                binding!!.settingVm!!.isNotiOn = true
                spUtil.isNotificationOn = true
            }
        }
    }

What's the problem??? 有什么问题??? I am not using two way binding and the ternary operator like @={} . 我没有使用双向绑定和@={}类的三元运算符。 But I reckon I should use two way binding because it is not a constant value. 但是我认为应该使用双向绑定,因为它不是一个常数。 And I have two images correctly. 而且我有两个正确的图像。

Someone says I should not use is prefix because it might generate getter and setter . 有人说我不应该使用is前缀,因为它可能产生gettersetter So, I even tried removing it and define has prefix or just NotiOn but didn't work. 因此,我什至尝试删除它并定义has前缀或仅NotiOn但没有用。

You can use a MutableLiveData in order to change the status and observe that inside the fragment to get live update and you can change the drawable values according to this LiveData 您可以使用MutableLiveData来更改状态,并观察片段内部以进行实时更新,并且可以根据此LiveData更改可绘制值

ViewModel.kt ViewModel.kt

class GuestViewModel @Inject constructor(val repo: GuestRepository) : ViewModel() {
    val guest = MutableLiveData<Guest>()
    val guestLoading = MutableLiveData<Boolean>()
}

Fragment.kt Fragment.kt

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final TestViewModel viewModel =          ViewModelProviders.of(getActivity()).get(TestViewModel.class);
    viewModel.guestLoading.observe(this, it -> {
         //Do something
    });
}

Please try this code once ## Your XML should be in the code is look like this ##请尝试一下此代码##您的XML应该在代码中如下所示

<ToggleButton 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:button="@{settingVm.isNotiOn ? @drawable/btn_on_mid : @drawable/btn_off_mid}"/>

and the following java code you can reduce and get the result in proper form. 和以下Java代码,您可以减少并以正确的形式获得结果。 please use this code on fragment class. 请在片段类上使用此代码。

val spUtil = SharedPreferenceUtil(activity!!) tbutton.setOnCheckedChangeListener { buttonView, isChecked -> spUtil.isNotificationOn = isChecked binding!!.settingVm!!.isNotiOn = isChecked }

======================================================================== ================================================== ======================

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM