android:layout_width="wrap_content" - width matches the content of the element
android:layout_width="match_parent" - width matches the parent width (whole screen)
android:layout_width="0dp" - stretches it to the same size, so it covers the width of the parent
visibility="gone" - nezaberá miesto
visibility="invisible" - zaberá miesto
notifyDataSetChanged() - redraws everything
notify* - which returns row indices is the right choice
view.findViewById<Button>(R.id.btn).setOnClickListener {
// code goes here
}
class AuthViewModel(private val userRepository: UserRepository) : ViewModel() {
private val _loginResult = MutableLiveData<String>()
val loginResult: LiveData<String> get() = _loginResult
fun loginUser() {
viewModelScope.launch {
val result = userRepository.apiLoginUser(username.value ?: "", password.value ?: "")
_loginResult.postValue(result.first ?: "")
_userResult.postValue(result.second)
}
}
}
XML
<data>
<variable
name="model"
type="sk.stuba.fei.uim.mobv.viewmodels.AuthViewModel"
/>
</data>
private lateinit var viewModel: AuthViewModel
private var binding: FragmentLoginBinding? = null
Inserting viewmodel to fragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = ViewModelProvider(requireActivity(), object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return FeedViewModel(UserRepository.getInstance(requireContext())) as T
}
})[FeedViewModel::class.java]
}