GithubHelp home page GithubHelp logo

easyexlivedata's Introduction

EasyExLiveData

a easy case to study live data

step by step

  1. Add dependce in build.gradle

     // liveData and ViewModel
     implementation 'android.arch.lifecycle:viewmodel:1.1.1'
     implementation 'android.arch.lifecycle:extensions:1.1.1'
     annotationProcessor 'android.arch.lifecycle:compiler:1.1.1'
    
  2. Create your ViewModel e.g. LiveDataTimerViewModel

  3. Edit in your View class(Activity or fragment)

      // a. init ViewModel
      // b. create observer and override onChanged event
      // c. use viewModel.getYourLiveData.observe(this, observer)
    
  4. When liveData changed by post value or set value, View will triggered onChanged event to do something about update UI

Part of coding

ViewModel :

    public class LiveDataTimerViewModel extends ViewModel {
        private MutableLiveData<Long> mElapsedTime;
        ...
        // this function let View can observer liveData in this ViewModel
        public MutableLiveData<Long> getElapsedTime() {
            if (null == mElapsedTime){
                mElapsedTime = new MutableLiveData<>();
            }
            return mElapsedTime;
        }

View :

    public class MainActivity extends AppCompatActivity {
        private LiveDataTimerViewModel liveDataTimerViewModel;
        ...
        protected void onCreate(Bundle savedInstanceState) {
            // 1. init ViewModel
            liveDataTimerViewModel = ViewModelProviders.of(this).get(LiveDataTimerViewModel.class);
            // 2. create observers
            final Observer<Long> elapsedTimeObserver = new Observer<Long>() {
                @Override
                public void onChanged(@Nullable final Long aLong) {
                    // 5. update UI
                    ((TextView)findViewById(R.id.text1)).setText(aLong.toString());
                }
            };               
            // 3. observe LiveData by get it from ViewModel and bind lifeCycle of this View
            liveDataTimerViewModel.getElapsedTime().observe(this, elapsedTimeObserver);
        }
        // 4. ... do someMethod cause liveData change value

Demo

Android Demo

easyexlivedata's People

Contributors

u0652804 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.