Skip to content

ArthurVimond/RxBindingAdapters

Repository files navigation

RxBindingAdapters

Download

A collection of BindingAdapters for direct integration of RxJava with the Data Binding library.

Gradle Setup

Check that you have the jcenter repository in your Project's build.gradle file:

repositories {
    // ...
    jcenter()    
}

Add the dependency in your Module's build.gradle file:

dependencies {
    // ...
    implementation 'fr.arthurvimond.rxbindingadapters:core:0.2.0'
}

BindingAdapter List

View clicks

XML layout:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:rxClick="@{viewModel.buttonClicks}" />

ViewModel:

class MainViewModel() : ViewModel() {
  
    val buttonClicks: PublishSubject<Empty> = PublishSubject.create()
    
    // ...
  
}

View long clicks

XML layout:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:rxLongClick="@{viewModel.buttonLongClicks}" />

ViewModel:

class MainViewModel() : ViewModel() {
  
    val buttonLongClicks: PublishSubject<Empty> = PublishSubject.create()
    
    // ...
  
}

View touch events

XML layout:

<View
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:rxTouch="@{viewModel.touchEvents}" />

ViewModel:

class MainViewModel() : ViewModel() {
  
    val touchEvents: PublishSubject<MotionEvent> = PublishSubject.create()
    
    // ...
  
}

EditText text changes

XML layout:

<android.support.design.widget.TextInputEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:rxText="@{viewModel.username}" />

ViewModel:

class MainViewModel() : ViewModel() {
  
    val username: BehaviorSubject<String> = BehaviorSubject.createDefault("Arthur")
    
    // ...
  
}

CompoundButton checked changes

XML layout:

<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:rxChecked="@{viewModel.enabled}" />

ViewModel:

class MainViewModel() : ViewModel() {
  
    val enabled: BehaviorSubject<Boolean> = BehaviorSubject.createDefault(true)
    
    // ...
  
}

RadioGroup selection changes

XML layout:

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:rxItem="@{viewModel.gender}">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Male" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female" />

</RadioGroup>

ViewModel:

class MainViewModel() : ViewModel() {
  
    val gender: BehaviorSubject<String> = BehaviorSubject.createDefault("Male")
    
    // ...
  
}

Spinner selection changes

XML layout:

<android.support.v7.widget.AppCompatSpinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:rxItem="@{viewModel.favoriteLanguage}"
    app:rxItems="@{viewModel.getLanguageList}" />

ViewModel:

class MainViewModel() : ViewModel() {
  
    val favoriteLanguage: BehaviorSubject<String> = BehaviorSubject.createDefault("Kotlin")
    
    fun getLanguageList(): List<String> {
        return listOf("Kotlin", "Java", "Swift", "Dart", "JavaScript")
    }
    
    // ...
  
}

SeekBar progress changes (auto step)

XML layout:

<SeekBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:rxMax="@{viewModel.getMaxAge}"
    app:rxMin="@{viewModel.getMinAge}"
    app:rxProgress="@{viewModel.age}" />

ViewModel:

class MainViewModel() : ViewModel() {
  
    val age: BehaviorSubject<Int> = BehaviorSubject.createDefault(20)
    
    fun getMinAge(): Int {
        return 0
    }

    fun getMaxAge(): Int {
        return 115
    }
    
    // ...
  
}

Note: By default, the number of steps will be automatically assigned based on the min and max values. If you want to manually set a specific step number, add the app:rxStep XML attribute (see below).

SeekBar progress changes (custom step)

XML layout:

<SeekBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:rxMax="@{viewModel.getMaxAge}"
    app:rxMin="@{viewModel.getMinAge}"
    app:rxProgress="@{viewModel.age}"
    app:rxStep="@{viewModel.getSeekBarStep}" />

ViewModel:

class MainViewModel() : ViewModel() {
  
    val age: BehaviorSubject<Int> = BehaviorSubject.createDefault(20)
    
    fun getMinAge(): Int {
        return 0
    }

    fun getMaxAge(): Int {
        return 115
    }
    
    fun getSeekBarStep(): Int {
        return 10
    }
    
    // ...
  
}

License

Copyright 2018 Arthur VIMOND

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.