當前位置: 首頁>>代碼示例>>Java>>正文


Java ViewModelProviders類代碼示例

本文整理匯總了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;
}
 
開發者ID:coding-blocks,項目名稱:GSoC-Info-Android,代碼行數:19,代碼來源:MainPageFragment.java

示例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();
            }
        }
    });
}
 
開發者ID:rumaan,項目名稱:file.io-app,代碼行數:39,代碼來源:MainActivity.java

示例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);
}
 
開發者ID:linyongsheng,項目名稱:android-arch-mvvm,代碼行數:18,代碼來源:LoginActivity.java

示例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();
}
 
開發者ID:xiaobailong24,項目名稱:MVVMArms,代碼行數:21,代碼來源:WeatherDailyFragment.java

示例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();
}
 
開發者ID:firebase,項目名稱:friendlyeats-android,代碼行數:21,代碼來源:MainActivity.java

示例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();
}
 
開發者ID:armcha,項目名稱:MVP-Architecture-Components,代碼行數:18,代碼來源:BaseActivity.java

示例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();
}
 
開發者ID:armcha,項目名稱:MVP-Architecture-Components,代碼行數:18,代碼來源:BaseFragment.java

示例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());
}
 
開發者ID:GlobusLTD,項目名稱:recyclerview-android,代碼行數:23,代碼來源:MultipleChoiceModeExampleActivity.java

示例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();
        }
    });
}
 
開發者ID:AStA-HTWG-Konstanz,項目名稱:app-android,代碼行數:27,代碼來源:VmTestActivity.java

示例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());
}
 
開發者ID:GlobusLTD,項目名稱:recyclerview-android,代碼行數:27,代碼來源:SingleModalChoiceModeExampleActivity.java

示例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();
    });
}
 
開發者ID:willowtreeapps,項目名稱:android-instant-apps-demo,代碼行數:23,代碼來源:CartActivity.java

示例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);

}
 
開發者ID:sensorberg-dev,項目名稱:EasyIPC,代碼行數:27,代碼來源:EasyIpcArchActivity.java

示例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);
            }
        }
    });

}
 
開發者ID:BracketCove,項目名稱:RoomDemo2017,代碼行數:18,代碼來源:ListFragment.java

示例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());
            }

        }
    });


}
 
開發者ID:BracketCove,項目名稱:RoomDemo2017,代碼行數:22,代碼來源:DetailFragment.java

示例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();
}
 
開發者ID:googlesamples,項目名稱:android-architecture-components,代碼行數:18,代碼來源:UserFragment.java


注:本文中的android.arch.lifecycle.ViewModelProviders類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。