Android Jetpack-LiveData示例

示例1

public class NameViewModel extends ViewModel {
    // Create a LiveData with a String
    private MutableLiveData<String> currentName;
    public MutableLiveData<String> getCurrentName() {
        if (currentName == null) {
            currentName = new MutableLiveData<String>();
        }
        return currentName;
    }
    // Rest of the ViewModel...
}
public class MainActivity extends AppCompatActivity {
    private NameViewModel model;
    private TextView nameTextView;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameTextView = findViewById(R.id.nameTextView);
        button = findViewById(R.id.button);
        // Get the ViewModel.
        model = ViewModelProviders.of(this).get(NameViewModel.class);
        // Create the observer which updates the UI.
        final Observer<String> nameObserver = new Observer<String>() {
            @Override
            public void onChanged(@Nullable final String newName) {
                // Update the UI, in this case, a TextView.
                nameTextView.setText(newName);
            }
        };
        // Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
        model.getCurrentName().observe(this, nameObserver);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String anotherName = "John Doe";
                model.getCurrentName().setValue(anotherName);
            }
        });
    }
}

示例2

在第二个Activity修改LiveData对象的值,第一个Activity能实时观察LiveData对象的变化。

需要使用static标记currentName

public class NameViewModel extends ViewModel {
    private static MutableLiveData<String> currentName;
    public MutableLiveData<String> getCurrentName() {
        if (currentName == null) {
            currentName = new MutableLiveData<String>();
        }
        return currentName;
    }
}
public class SecActivity extends AppCompatActivity {

    private Button change;
    private TextView textView;
    private NameViewModel nameViewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sec);
        nameViewModel = ViewModelProviders.of(this).get(NameViewModel.class);
        change = findViewById(R.id.change);
        textView = findViewById(R.id.text);


        final Observer<String> nameObserver = new Observer<String>() {
            @Override
            public void onChanged(@Nullable final String newName) {
                textView.setText(newName);
            }
        };

        nameViewModel.getCurrentName().observe(this, nameObserver);

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nameViewModel.getCurrentName().setValue("jack");
            }
        });
    }

}
发布了383 篇原创文章 · 获赞 54 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/hongxue8888/article/details/103768481