Library
def lifecycle_version = "1.1.1" // ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable
name="viewModel"
type="com.app.testapp.UserProfileViewModel" /> </data> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:textSize="30sp"
android:hint="hint" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="@{()->viewModel.buttonClick()}"
android:layout_marginTop="36dp" /> </LinearLayout> </LinearLayout> </layout>
MainActivity.java
package com.app.testapp; import android.arch.lifecycle.Observer; import android.arch.lifecycle.ViewModelProviders; import android.databinding.DataBindingUtil; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.app.testapp.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { UserProfileViewModel userProfileViewModel; TextView textView; // Button button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); userProfileViewModel = ViewModelProviders.of(MainActivity.this).get(UserProfileViewModel.class); ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); binding.setViewModel(userProfileViewModel); binding.setLifecycleOwner(this); setContentView(binding.getRoot()); textView = (TextView) findViewById(R.id.textView); // button2 = (Button)findViewById(R.id.button2); // String s = userProfileViewModel.user.name + "\n" + userProfileViewModel.user.company;// textView.setText(s); // button2.setOnClickListener(new View.OnClickListener() {// @Override// public void onClick(View v) {////// userProfileViewModel.getUser().setValue(new User("ABC", "XYZ"));//// userProfileViewModel.user.company = "XYZ";// }// }); // "abc".toLowerCase() userProfileViewModel.getUser().observe(this, new Observer<User>() { @Override public void onChanged(@Nullable User user) { textView.setText(user.name); } } ); } }
UserProfileViewModel.Java
package com.app.testapp; import android.arch.lifecycle.LiveData; import android.arch.lifecycle.MutableLiveData; import android.arch.lifecycle.ViewModel; import android.widget.Toast; public class UserProfileViewModel extends ViewModel { // User user = new User("No Name", "No Com"); private MutableLiveData<User> user = new MutableLiveData<User>(); LiveData<User> userLiveData; public MutableLiveData<User> getUser() { return user; } public void buttonClick(){ getUser().setValue(new User("ABC", "XYZ")); } }User.javapackage com.app.testapp; public class User { String name; String company; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public User(String name, String company) { this.name = name; this.company = company; } }
22
No comments:
Post a Comment