本文整理汇总了Java中android.arch.lifecycle.ViewModelProviders类的典型用法代码示例。如果您正苦于以下问题:Java ViewModelProviders类的具体用法?Java ViewModelProviders怎么用?Java ViewModelProviders使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ViewModelProviders类属于android.arch.lifecycle包,在下文中一共展示了ViewModelProviders类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onCreateView
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_about, container, false);
RecyclerView recyclerView = view.findViewById(R.id.rv_timeline);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
MainPageViewModel mainPageViewModel = ViewModelProviders.of(getActivity()).get(MainPageViewModel.class);
timelineAdapter = new TimelineAdapter();
recyclerView.setAdapter(timelineAdapter);
mainPageViewModel.getMainPageCopy().observe(this, new Observer<MainPage.Copy>() {
@Override
public void onChanged(@Nullable MainPage.Copy copy) {
timelineAdapter.setData(copy);
}
});
return view;
}
示例2: onCreate
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
/* Animate the views */
MaterialIn.animate(rootView);
/* Get the view model */
uploadItemViewModel = ViewModelProviders.of(this).get(UploadItemViewModel.class);
/* Handle incoming intent content */
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
// FIXME: Google photos URI
if (type != null) {
Log.d(TAG, "Receive Type: " + type);
Uri fileUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
Log.d(TAG, "\nURI: " + fileUri);
if (Intent.ACTION_SEND.equals(action) && fileUri != null) {
handleExplicitFileShare(intent);
}
}
linkTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Copy the content of the link text to Clipboard
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("link", linkTextView.getText());
if (clipboardManager != null) {
clipboardManager.setPrimaryClip(clipData);
Toast.makeText(MainActivity.this, getString(R.string.link_copy), Toast.LENGTH_SHORT).show();
}
}
});
}
示例3: onCreate
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
@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);
}
示例4: initView
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
@Override
public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//获取 Activity 的ViewModel 来共享数据
mWeatherViewModel = ViewModelProviders.of(getActivity(), mViewModelFactory).get(WeatherViewModel.class);
mViewModel = ViewModelProviders.of(this, mViewModelFactory).get(WeatherDailyViewModel.class);
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_weather_daily, container, false);
//设置ViewModel
mBinding.setViewModel(mViewModel);
mBinding.retry.setViewModel(mViewModel);
mBinding.retry.setRetry(mViewModel);
mBinding.weatherSource.setViewModel(mViewModel);
//RecyclerView设置Adapter
mBinding.recyclerWeatherDaily.setAdapter(mAdapter);
//设置Refresh
mBinding.refresh.setColorSchemeColors(
ContextCompat.getColor(getContext(), R.color.colorPrimary),
ContextCompat.getColor(getContext(), R.color.colorAccent),
ContextCompat.getColor(getContext(), R.color.colorPrimaryDark));
return mBinding.getRoot();
}
示例5: onCreate
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
// View model
mViewModel = ViewModelProviders.of(this).get(MainActivityViewModel.class);
// Enable Firestore logging
FirebaseFirestore.setLoggingEnabled(true);
// Initialize Firestore and the main RecyclerView
initFirestore();
initRecyclerView();
// Filter Dialog
mFilterDialog = new FilterDialogFragment();
}
示例6: onCreate
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@CallSuper
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BaseViewModel<V, P> viewModel = ViewModelProviders.of(this).get(BaseViewModel.class);
boolean isPresenterCreated = false;
if (viewModel.getPresenter() == null) {
viewModel.setPresenter(initPresenter());
isPresenterCreated = true;
}
presenter = viewModel.getPresenter();
presenter.attachLifecycle(getLifecycle());
presenter.attachView((V) this);
if (isPresenterCreated)
presenter.onPresenterCreated();
}
示例7: onViewCreated
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@CallSuper
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
BaseViewModel<V, P> viewModel = ViewModelProviders.of(this).get(BaseViewModel.class);
boolean isPresenterCreated = false;
if (viewModel.getPresenter() == null) {
viewModel.setPresenter(initPresenter());
isPresenterCreated = true;
}
presenter = viewModel.getPresenter();
presenter.attachLifecycle(getLifecycle());
presenter.attachView((V) this);
if (isPresenterCreated)
presenter.onPresenterCreated();
}
示例8: onCreate
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiple_choice_mode_example);
final PersonsViewModel viewModel = ViewModelProviders.of(this).get(PersonsViewModel.class);
final Datasource<Person> datasource = viewModel.getDatasource();
final CheckedPersonsAdapter adapter = new CheckedPersonsAdapter(datasource, true);
mRecyclerView = (RecyclerView) findViewById(android.R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(adapter);
mChoiceMode = new MultipleChoiceMode(savedInstanceState);
mChoiceMode.setChoiceModeListener(this::onItemCheckedChanged);
mChoiceModeHelper = new ChoiceModeHelper<>(adapter, mChoiceMode);
mChoiceModeHelper.setRecyclerView(mRecyclerView);
findViewById(R.id.action_clear_choices).setOnClickListener(v -> mChoiceMode.clearChoices());
findViewById(R.id.action_get_checked_items).setOnClickListener(v -> showCheckedItems());
}
示例9: onCreate
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vm_test);
ButterKnife.bind(this);
testViewModel = ViewModelProviders.of(this).get(TestViewModel.class);
testViewModel.getMutableIntList().observe(this, list -> {
Timber.d("trigger %s", list);
if (list != null) {
textNumList.setText(list.toString());
}
});
setSupportActionBar(toolbar);
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
示例10: onCreate
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_modal_choice_mode_example);
mViewModel = ViewModelProviders.of(this).get(PersonsViewModel.class);
final Datasource<Person> datasource = mViewModel.getDatasource();
final ActivatedPersonsAdapter adapter = new ActivatedPersonsAdapter(datasource);
mRecyclerView = (RecyclerView) findViewById(android.R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(adapter);
final ActionModeCompat actionMode = ActionModeCompat.from(this);
mChoiceMode = new SingleModalChoiceMode(actionMode, mModalChoiceModeListener, savedInstanceState);
// mChoiceMode.setStartOnSingleTapEnabled(true);
// mChoiceMode.setFinishActionModeOnClearEnabled(false);
mChoiceModeHelper = new ChoiceModeHelper<>(adapter, mChoiceMode);
mChoiceModeHelper.setOnItemClickListener(this::onItemClick);
mChoiceModeHelper.setRecyclerView(mRecyclerView);
findViewById(R.id.action_clear_choices).setOnClickListener(v -> mChoiceMode.clearChoices());
findViewById(R.id.action_get_checked_item).setOnClickListener(v -> showCheckedItem());
}
示例11: onCreate
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
viewModel = ViewModelProviders.of(this).get(CartViewModel.class);
fab = (FloatingActionButton) findViewById(R.id.fab);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setupToolbar();
handleDeepLink();
fab.setImageDrawable(VectorDrawableCompat.create(getResources(), R.drawable.ic_share_white_24dp, null));
fab.setOnClickListener(view -> {
String cartId = viewModel.getCartId().getValue();
ShareCompat.IntentBuilder.from(this)
.setText(String.format(Locale.US, "Check out my shopping cart now using Android Instant Apps! \n%s/cart/%s", ROOT_ENDPOINT, cartId))
.setType("text/plain")
.setChooserTitle(share_cart)
.startChooser();
});
}
示例12: onCreate
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerView.setAdapter(adapter = new Adapter());
viewModel = ViewModelProviders
.of(this)
.get(IpcViewModel.class)
.init(MainService.class, BIND_AUTO_CREATE);
viewModel.dispatchEvent(new EarlyData("sent from the activity")); // get's queued
viewModel
.getLiveData(Data.class)
.observe(ProcessLifecycleOwner.get(), onData);
viewModel
.getLiveData(Data.List.class)
.observe(this, onDataList);
viewModel
.getLiveData(EarlyData.class)
.observe(this, onEarlyData);
}
示例13: onActivityCreated
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Set up and subscribe (observe) to the ViewModel
listItemCollectionViewModel = ViewModelProviders.of(this, viewModelFactory)
.get(ListItemCollectionViewModel.class);
listItemCollectionViewModel.getListItems().observe(this, new Observer<List<ListItem>>() {
@Override
public void onChanged(@Nullable List<ListItem> listItems) {
if (ListFragment.this.listOfData == null) {
setListData(listItems);
}
}
});
}
示例14: onActivityCreated
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Set up and subscribe (observe) to the ViewModel
listItemViewModel = ViewModelProviders.of(this, viewModelFactory)
.get(ListItemViewModel.class);
listItemViewModel.getListItemById(itemId).observe(this, new Observer<ListItem>() {
@Override
public void onChanged(@Nullable ListItem listItem) {
if (listItem != null) {
dateAndTime.setText(listItem.getItemId());
message.setText(listItem.getMessage());
coloredBackground.setImageResource(listItem.getColorResource());
}
}
});
}
示例15: onActivityCreated
import android.arch.lifecycle.ViewModelProviders; //导入依赖的package包/类
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
userViewModel = ViewModelProviders.of(this, viewModelFactory).get(UserViewModel.class);
userViewModel.setLogin(getArguments().getString(LOGIN_KEY));
userViewModel.getUser().observe(this, userResource -> {
binding.get().setUser(userResource == null ? null : userResource.data);
binding.get().setUserResource(userResource);
// this is only necessary because espresso cannot read data binding callbacks.
binding.get().executePendingBindings();
});
RepoListAdapter rvAdapter = new RepoListAdapter(dataBindingComponent, false,
repo -> navigationController.navigateToRepo(repo.owner.login, repo.name));
binding.get().repoList.setAdapter(rvAdapter);
this.adapter = new AutoClearedValue<>(this, rvAdapter);
initRepoList();
}