android.arch.lifecycle.MutableLiveData.observe()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(199)

本文整理了Java中android.arch.lifecycle.MutableLiveData.observe()方法的一些代码示例,展示了MutableLiveData.observe()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MutableLiveData.observe()方法的具体详情如下:
包路径:android.arch.lifecycle.MutableLiveData
类名称:MutableLiveData
方法名:observe

MutableLiveData.observe介绍

暂无

代码示例

代码示例来源:origin: commonsguy/cw-omnibus

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setHasOptionsMenu(true);
 ShoutingEchoService.STATUS.observe(this,
  status -> {
   if (server!=null && status!=null) server.setChecked(status.isRunning);
  });
}

代码示例来源:origin: k9mail/k-9

@MainThread
public void observe(@NonNull LifecycleOwner owner, @NonNull final Observer<T> observer) {
  if (hasActiveObservers()) {
    Timber.w("Multiple observers registered but only one will be notified of changes.");
  }
  // Observe the internal MutableLiveData
  super.observe(owner, new Observer<T>() {
    @Override
    public void onChanged(@Nullable T t) {
      if (pending.compareAndSet(true, false)) {
        observer.onChanged(t);
      }
    }
  });
}

代码示例来源:origin: JeremyLiao/LiveDataBus

public void observeSticky(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
  super.observe(owner, observer);
}

代码示例来源:origin: JeremyLiao/LiveDataBus

public void observeSticky(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
  super.observe(owner, observer);
}

代码示例来源:origin: REBOOTERS/My-MVP

private void subscribe() {
    mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeAdapterListener(){
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        super.onProgressChanged(seekBar, progress, fromUser);
        if (fromUser) {
          mSeekbarViewModel.seekbarValue.setValue(progress);
        }
      }
    });

    mSeekbarViewModel.seekbarValue.observe(this, new Observer<Integer>() {
      @Override
      public void onChanged(@Nullable Integer integer) {
        if (integer != null) {
          mSeekBar.setProgress(integer);
        }
      }
    });
  }
}

代码示例来源:origin: simplezhli/Saber

@Override
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
  super.observe(owner, observer);
  try {
    hook(observer);
  } catch (Exception e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: REBOOTERS/My-MVP

private void subscribe() {
    Observer<Long> elapsedTimerObserver = new Observer<Long>() {
      @Override
      public void onChanged(@Nullable Long aLong) {
        String value = String.valueOf(aLong);
        mTimerText.setText(value);

        Log.e(TAG, "onChanged: aLong==" + aLong);

      }
    };

    mLiveDataTimerViewModel.getElapsedTime().observe(this, elapsedTimerObserver);
  }
}

代码示例来源:origin: simplezhli/Saber

@MainThread
@Override
public void observe(LifecycleOwner owner, final Observer<T> observer) {
  if (hasActiveObservers()) {
    Log.w(TAG, "Multiple observers registered but only one will be notified of changes.");
  }
  // Observe the internal MutableLiveData
  super.observe(owner, new Observer<T>() {
    @Override
    public void onChanged(@Nullable T t) {
      if (mPending.compareAndSet(true, false)) {
        observer.onChanged(t);
      }
    }
  });
}

代码示例来源:origin: AliEsaAssadi/android-mvvm-sample-app

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ButterKnife.bind(this);
  movieAdapter = new MovieAdapter(this);
  recyclerView.setAdapter(movieAdapter);
  viewModel = createViewModel();
  viewModel.getLoadingStatus().observe(this, new LoadingObserver());
  viewModel.getMovies().observe(this, new MovieObserver());
}

代码示例来源:origin: linyongsheng/android-arch-mvvm

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.login_activity_layout);
  // step0 获取相关的viewModel
  mLoginViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
  // step1 获取view
  mLoginBtn = (Button) findViewById(R.id.loginBtn);
  // step2 设置监听
  mLoginBtn.setOnClickListener(this);
  // setp3 绑定数据
  mLoginViewModel.loginResult.observe(this, mLoginObserver);
}

代码示例来源:origin: AliEsaAssadi/android-mvvm-sample-app

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_details);
  ButterKnife.bind(this);
  DetailsViewModel viewModel = createViewModel();
  viewModel.getMovie().observe(this, new MovieObserver());
  viewModel.loadMovieData(getIntent());
}

代码示例来源:origin: HarinTrivedi/Easy.Api

public void observeLoadingState(MutableLiveData<STATE> state) {
 if (state != null) {
  state.observe(this, state1 -> {
   switch (Objects.requireNonNull(state1)) {
    case SHOW_LOADING:
     showLoading();
     break;
    case HIDE_LOADING:
     hideLoading();
     break;
    case ERROR:
     showError("");
     break;
    case NO_INTERNET:
     showNoInternet();
     break;
   }
  });
 }
}

代码示例来源:origin: anitaa1990/TrailersApp

private void initialiseViewModel() {
  moviesListViewModel = ViewModelProviders.of(this, viewModelFactory).get(MovieListViewModel.class);
  moviesListViewModel.setType(MENU_MOVIE_ITEM.get(getArguments() == null ? 0: getArguments().getInt(INTENT_CATEGORY)));
  moviesListViewModel.getMoviesLiveData().observe(this, resource -> {
    if(resource.isLoading()) {
    } else if(!resource.data.isEmpty()) {
      updateMoviesList(resource.data);
    } else handleErrorResponse();
  });
}

代码示例来源:origin: anitaa1990/TrailersApp

private void initialiseViewModel() {
  tvListViewModel = ViewModelProviders.of(this, viewModelFactory).get(TvListViewModel.class);
  tvListViewModel.setType(MENU_TV_ITEM.get(getArguments() == null ? 0: getArguments().getInt(INTENT_CATEGORY)));
  tvListViewModel.getTvsLiveData().observe(this, resource -> {
    if(resource.isLoading()) {
    } else if(!resource.data.isEmpty()) {
      updateTvsList(resource.data);
    } else handleErrorResponse();
  });
}

代码示例来源:origin: anitaa1990/TrailersApp

private void querySearch(String text) {
  searchViewModel.searchTv(text);
  searchViewModel.getTvsLiveData().observe(this, resource -> {
    if(resource.isLoading()) {
      displayLoader();
    } else if(resource.data != null && !resource.data.isEmpty()) {
      handleSuccessResponse(resource.data);
    } else handleErrorResponse();
  });
}

代码示例来源:origin: anitaa1990/TrailersApp

private void querySearch(String text) {
  searchViewModel.searchMovie(text);
  searchViewModel.getMoviesLiveData().observe(this, resource -> {
    if(resource.isLoading()) {
      displayLoader();
    } else if(resource.data != null && !resource.data.isEmpty()) {
      handleSuccessResponse(resource.data);
    } else handleErrorResponse();
  });
}

代码示例来源:origin: commonsguy/cw-androidarch

@Override
public void onActivityCreated(final Bundle state) {
 super.onActivityCreated(state);
 getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
 if (adapter==null) {
  adapter=new EventLogAdapter();
 }
 setListAdapter(adapter);
 ScheduledService.BUS.observe(this, intent -> adapter.add(intent));
}

代码示例来源:origin: JeremyLiao/LiveDataBus

@Override
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
  //保存LifecycleOwner的当前状态
  Lifecycle lifecycle = owner.getLifecycle();
  Lifecycle.State currentState = lifecycle.getCurrentState();
  int observerSize = getLifecycleObserverMapSize(lifecycle);
  boolean needChangeState = currentState.isAtLeast(Lifecycle.State.STARTED);
  if (needChangeState) {
    //把LifecycleOwner的状态改为INITIALIZED
    setLifecycleState(lifecycle, Lifecycle.State.INITIALIZED);
    //set observerSize to -1,否则super.observe(owner, observer)的时候会无限循环
    setLifecycleObserverMapSize(lifecycle, -1);
  }
  super.observe(owner, observer);
  if (needChangeState) {
    //重置LifecycleOwner的状态
    setLifecycleState(lifecycle, currentState);
    //重置observer size,因为又添加了一个observer,所以数量+1
    setLifecycleObserverMapSize(lifecycle, observerSize + 1);
    //把Observer置为active
    hookObserverActive(observer, true);
  }
  //更改Observer的version
  hookObserverVersion(observer);
}

代码示例来源:origin: HarinTrivedi/Easy.Api

@Override
protected void initializeComponent(View view) {
 viewModel = ViewModelProviders.of(this).get(UserViewModel.class);
 adapter = new UserListAdapter();
 binding.rvUsers.setAdapter(adapter);
 binding.tab.addOnTabSelectedListener(this);
 viewModel.getUsersLiveData().observe(this, users -> {
  if (users != null && users.size() > 0) {
   hideLoading();
   adapter.setUserList(users);
  } else {
   showLoading();
  }
  binding.executePendingBindings();
 });
 observeLoadingState(viewModel.getLoadingStateLiveData());
 fetchUsers();
}

代码示例来源:origin: anitaa1990/TrailersApp

private void initialiseViewModel() {
  movieDetailViewModel = ViewModelProviders.of(this, viewModelFactory).get(MovieDetailViewModel.class);
  movieDetailViewModel.fetchMovieDetail(getIntent().getParcelableExtra(INTENT_MOVIE));
  movieDetailViewModel.getMovieDetailsLiveData().observe(this, movieEntity -> {
    updateMovieDetailView(movieEntity);
    if(movieEntity.getVideos() != null && !movieEntity.getVideos().isEmpty()) {
      updateMovieVideos(movieEntity.getVideos());
    }
    if(movieEntity.getCrews() != null && !movieEntity.getCrews().isEmpty()) {
      updateMovieCrewDetails(movieEntity.getCrews());
    }
    if(movieEntity.getCasts() != null && !movieEntity.getCasts().isEmpty()) {
      binding.expandButton.setVisibility(View.VISIBLE);
      updateMovieCastDetails(movieEntity.getCasts());
    }
    if(movieEntity.getSimilarMovies() != null && !movieEntity.getSimilarMovies().isEmpty()) {
      updateSimilarMoviesView(movieEntity.getSimilarMovies());
    }
    if(movieEntity.getReviews() != null && !movieEntity.getReviews().isEmpty()) {
      updateMovieReviews(movieEntity.getReviews());
    } else binding.includedReviewsLayout.reviewView.setVisibility(View.GONE);
  });
}

相关文章