-
-
Save Richie97/2be3d6f0cc2565d66929e0ad4f96be21 to your computer and use it in GitHub Desktop.
ViewModel Databinding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layout xmlns:android="http://schemas.android.com/apk/res/android"> | |
<data> | |
<variable | |
name="viewModel" | |
type="com.willowtreeapps.android.elevatorroom.BarometerViewModel" /> | |
</data> | |
<FrameLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:text="@{viewModel.getPressure}" /> | |
</FrameLayout> | |
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BarometerActivity extends LifecycleActivity { | |
private BarometerViewModel viewModel; | |
@Override | |
public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
ActivityBarometerBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_barometer); | |
viewModel = ViewModelProviders.of(this).get(BarometerViewModel.class); | |
viewModel.startObserving(this); | |
binding.setViewModel(viewModel); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BarometerViewModel extends AndroidViewModel implements Observable { | |
private BarometerLiveData data; | |
public BarometerViewModel(Application app) { | |
super(app); | |
data = new BarometerLiveData(getApplication()); // You can either get a context through your app, or better yet inject it with Dagger. | |
} | |
public void startObserving(LifecycleOwner lifecycleOwner) { | |
data.observe(lifecycleOwner, sensorData -> notifyPropertyChanged(BR.pressure)); | |
} | |
@Bindable | |
public String getPressure() { | |
return "SensorData: " + data.getValue(); | |
} | |
//This is all just copied from BaseObservable so we can use this ViewModel as our backing Databinding model | |
private transient PropertyChangeRegistry mCallbacks; | |
@Override | |
public void addOnPropertyChangedCallback(OnPropertyChangedCallback callback) { | |
synchronized (this) { | |
if (mCallbacks == null) { | |
mCallbacks = new PropertyChangeRegistry(); | |
} | |
} | |
mCallbacks.add(callback); | |
} | |
@Override | |
public void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) { | |
synchronized (this) { | |
if (mCallbacks == null) { | |
return; | |
} | |
} | |
mCallbacks.remove(callback); | |
} | |
public void notifyChange() { | |
synchronized (this) { | |
if (mCallbacks == null) { | |
return; | |
} | |
} | |
mCallbacks.notifyCallbacks(this, 0, null); | |
} | |
public void notifyPropertyChanged(int fieldId) { | |
synchronized (this) { | |
if (mCallbacks == null) { | |
return; | |
} | |
} | |
mCallbacks.notifyCallbacks(this, fieldId, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment