简体   繁体   English

选择器不响应点击事件

[英]Selector doesn't respond to click event

I've tried understanding why 我试着理解为什么

<Button
            android:id="@+id/partTime"
            style="@style/FilterFragment.JobTypeButton"
            android:layout_marginStart="8dp"
            android:onClick="@{(view) -> vm.updateJobType(view.getId())}"
            android:text="@string/part_time"
            app:layout_constraintStart_toEndOf="@+id/fullTime"
            app:layout_constraintTop_toTopOf="@+id/fullTime" />

with the style 与风格

<style name="FilterFragment"/>
<style name="FilterFragment.JobTypeButton" parent="TextLittleButtonWhite">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@drawable/background_job_type</item>
    <item name="android:padding">8dp</item>
    <item name="android:drawableLeft">@drawable/add</item>
    <item name="android:drawablePadding">8dp</item>
    <item name="android:textAllCaps">false</item>
</style>

whose background is a selector 其背景是选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/background_job_type_selected" android:state_pressed="true" android:state_selected="true" />
<item android:drawable="@drawable/background_job_type_selected" android:state_pressed="true" />
<item android:drawable="@drawable/background_job_type_selected" android:state_selected="true" />
<item android:drawable="@drawable/background_job_type_unselected" />

doesn't update when selected. 选择后不会更新。 I can't find something wrong with any of them, and if you select a button, destroy the fragment, and return to the original fragment, then the appropriate background is displayed. 我找不到其中的任何错误,如果选择一个按钮,销毁片段,然后返回原始片段,则会显示适当的背景。 Why isn't the background updating when the button is selected? 为什么选择按钮后背景没有更新?

The solution was to create 解决方案是创建

@BindingAdapter("selected")
fun Button.select(selected: Boolean) {
    isSelected = selected
}

and add it to the button like so (bottom attribute) 并将其添加到按钮中(底部属性)

<Button
            android:id="@+id/partTime"
            style="@style/FilterFragment.JobTypeButton"
            android:layout_marginStart="8dp"
            android:onClick="@{(view) -> vm.updateJobType(view.getId())}"
            android:text="@string/part_time"
            app:layout_constraintStart_toEndOf="@+id/fullTime"
            app:layout_constraintTop_toTopOf="@+id/fullTime"
            app:selected="@{vm.isPartTime}" />

The onClick updates isPartTime in my viewmodel, which is observed by app:selected . onClick更新是我的视图isPartTime中的isPartTime ,由app:selected观察。 vm.isPartTime binds my viewmodel's ObservableBoolean to the button. vm.isPartTime将我的视图模型的ObservableBoolean绑定到该按钮。

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

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