简体   繁体   中英

Problem with ternary conditional in xml with data-binding

I have the 2 following situation around this button with some params being set by data-binding and I've found that the problem is about a ternary operation with the true and false results order.

<Button
    android:id="@+id/tbIbArrowBack"
    android:layout_width="@dimen/extra_large_size_32"
    android:layout_height="@dimen/extra_large_size_32"
    android:layout_marginStart="@dimen/activity_default_medium_margin"
    android:background="@drawable/ic_arrow_back_white_24dp"
    android:onClick="@{() -> manager.onClick()}"
    android:visibility="@{manager.showBackButton ? View.VISIBLE : View.GONE,  default=gone}"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:visibility="visible" />
  1. this code won't copile: android:background="@{manager.backButtonColor == 0? manager.backButtonColor: @drawable/ic_arrow_back_white_24dp,default=@drawable/ic_arrow_back_white_24dp}
  2. this code compile: android:background="@{manager.backButtonColor?= 0: @drawable/ic_arrow_back_white_24dp. manager,backButtonColor, default=@drawable/ic_arrow_back_white_24dp}"

the item 1 and 2 are how I tried to do a customizable background. While @drawable/ic_arrow_back_white_24dp is in second position of ternary operation it won't work, but while it is in first position of ternary operation it work. Can someone explain to me why 1 won't compile?

What is the data type of "manager.backButtonColor"? I think you are using 2 different data type for the binding.

Could you just try out like this

@{data.isSelectMessage ? @drawable/ic_menu_message_selected : @drawable/ic_menu_message_deselected}

because it perfectly works for me and I used it like this.

<ImageView
       android:id="@+id/img_menu_message"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center"
       android:alpha="@{alpha}"
       android:background="@color/white"
       android:src="@{data.isSelectMessage ? @drawable/ic_menu_message_selected : @drawable/ic_menu_message_deselected}"
       tools:src="@drawable/ic_menu_message_selected" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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