简体   繁体   中英

Switch case with data binding

Previously I had created an interface with two button implemented data binding to have a case when button A clicked then Shows input 1, if button B clicked then show input 1+2. Something similar to selected flight trip (one way or return).

图像案例1

the structure I use for this is a boolean

android:onClick="@{() -> view.selectTripType(true)}"

android:onClick="@{() -> view.selectTripType(false)}"

and in my fragment is create a function with

 public void selectTripType(boolean isRound) {
        viewModel.setRoundTrip(isRound);
        if (isRound) {
            setTripBtnStyle(binding.fragmentBusmainBtnroundtrip, binding.fragmentBusmainBtnoneway);
            binding.fragmentBusmainReturndateT.setVisibility(View.VISIBLE);
        } else {
            setTripBtnStyle(binding.fragmentBusmainBtnoneway, binding.fragmentBusmainBtnroundtrip);
            binding.fragmentBusmainReturndateT.setVisibility(View.GONE);
        }
    }

Now, I want to add another button which will have 3rd case, can I use a switch case in a data binding or how should I create for this?

3按钮与差异案例

How about using some integer value?

android:onClick="@{() -> view.selectTripType(1)}"

android:onClick="@{() -> view.selectTripType(2)}"

android:onClick="@{() -> view.selectTripType(3)}"

and in your function

public void selectTripType(int type) {
    // 1 : one way
    // 2 : round
    // 3 : disposal
}

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