Created
December 9, 2018 12:09
-
-
Save Pooh3Mobi/199f5e8763c556d262bcfbb7c10b6eef to your computer and use it in GitHub Desktop.
DataBinding like accessing Model Fields in Layout.
This file contains hidden or 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" xmlns:tools="http://schemas.android.com/tools" | |
xmlns:app="http://schemas.android.com/apk/res-auto"> | |
<data> | |
<import type="java.lang.Integer" /> | |
<variable name="vm" type="app.example.vmdb_sample.MainViewModel" /> | |
</data> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:showIn="@layout/activity_main" | |
tools:context=".MainActivityFragment"> | |
<TextView | |
android:text="@{vm.userLiveData.name}" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/textViewUserName" | |
app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="0.5" | |
app:layout_constraintEnd_toStartOf="@+id/textViewUserAge" android:layout_marginTop="52dp" | |
app:layout_constraintTop_toTopOf="parent"/> | |
<TextView | |
android:text="@{Integer.toString(vm.userLiveData.age)}" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/textViewUserAge" app:layout_constraintStart_toEndOf="@+id/textViewUserName" | |
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintBaseline_toBaselineOf="@+id/textViewUserName"/> | |
<Button | |
android:text="Button" | |
android:onClick="@{() -> vm.load()}" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/button" android:layout_marginTop="8dp" | |
app:layout_constraintTop_toBottomOf="@+id/textViewUserName" android:layout_marginBottom="8dp" | |
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" | |
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" | |
android:layout_marginEnd="8dp" | |
app:layout_constraintHorizontal_bias="0.21" app:layout_constraintVertical_bias="0.51"/> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</layout> |
This file contains hidden or 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
class MainActivityFragment : androidx.fragment.app.Fragment() { | |
val viewModel = MainViewModel() | |
override fun onCreateView( | |
inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
val binding = FragmentMainBinding.inflate(inflater) | |
binding.setLifecycleOwner(this) | |
binding.vm = viewModel | |
return binding.root | |
} | |
} | |
class MainViewModel : ViewModel() { | |
val handler: Handler = Handler() | |
val userLiveData = UserLiveData("Bar", 22) | |
fun load() { | |
Executors.newSingleThreadExecutor().execute { | |
updateUser((User("Foo", 50))) | |
} | |
} | |
private fun updateUser(user: User) { | |
handler.post { | |
Log.d("aaa", "load:$user") | |
userLiveData.value = user | |
} | |
} | |
} | |
data class User(val name: String, val age: Int) | |
class UserLiveData(val defaultName: String, val defaultAge: Int) { | |
val userLiveData = MutableLiveData<User>() | |
val name = MediatorLiveData<String>().apply { value = defaultName } | |
val age = MediatorLiveData<Int>().apply { value = defaultAge } | |
init { | |
name.addSource(userLiveData) { liveData: User -> name.value = liveData.name } | |
age.addSource(userLiveData) { liveData: User -> age.value = liveData.age } | |
} | |
var value: User = User(defaultName, defaultAge) | |
set(value) { | |
if (value == field) return | |
field = value | |
name.value = value.name | |
age.value = value.age | |
} | |
fun post(user: User) { | |
name.postValue(user.name) | |
age.postValue(user.age) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment