小編給大家分享一下Android如何通過(guò)ViewModel保存數(shù)據(jù)實(shí)現(xiàn)多頁(yè)面的數(shù)據(jù)共享功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
在海州等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,營(yíng)銷型網(wǎng)站建設(shè),外貿(mào)網(wǎng)站建設(shè),海州網(wǎng)站建設(shè)費(fèi)用合理。
通過(guò)ViewModel實(shí)現(xiàn)的數(shù)據(jù)共享符合Android的MVC設(shè)計(jì)模式,將數(shù)據(jù)獨(dú)立出來(lái)
實(shí)現(xiàn)的Demo
1、主頁(yè)面通過(guò)SeekBar 來(lái)改變數(shù)字的值
2、點(diǎn)擊進(jìn)入就進(jìn)入第二個(gè)界面,但是數(shù)據(jù)還是共享的
3、隨便加兩個(gè)數(shù)字上去,再次切換
4、發(fā)現(xiàn)數(shù)據(jù)還是共享的
下面是具體實(shí)現(xiàn)步驟:
1、建立兩個(gè)Fragment(使用了Binding 和 Navigation)
一點(diǎn)要添加Binding 和 Navigation 不然做不了
2、建立一個(gè)繼承于ViewModel的類
3、分別在兩個(gè)Fragment的代碼中使用繼承于ViewModel的那個(gè)類,就可以實(shí)現(xiàn)數(shù)據(jù)共享
下面是具體代碼:
1、繼承于ViewModel的類
package com.example.naviation01;import androidx.lifecycle.MutableLiveData;import androidx.lifecycle.ViewModel;public class MyViewMode extends ViewModel { private MutableLiveData
2、Fragment 主頁(yè)
package com.example.naviation01;import android.os.Bundle;import androidx.databinding.DataBindingUtil;import androidx.fragment.app.Fragment;import androidx.fragment.app.FragmentController;import androidx.lifecycle.ViewModel;import androidx.lifecycle.ViewModelProvider;import androidx.lifecycle.ViewModelProviders;import androidx.navigation.NavController;import androidx.navigation.Navigation;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.SeekBar;import com.example.naviation01.databinding.FragmentHomeBinding;/** * A simple {@link Fragment} subclass. */public class HomeFragment extends Fragment { public HomeFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment final MyViewMode myViewMode; myViewMode = ViewModelProviders.of(getActivity()).get(MyViewMode.class); FragmentHomeBinding binding; binding = DataBindingUtil.inflate(inflater,R.layout.fragment_home,container,false); binding.setData(myViewMode); binding.setLifecycleOwner(getActivity()); binding.seekBar.setProgress(myViewMode.getNumber().getValue()); binding.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { myViewMode.getNumber().setValue(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); binding.button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NavController controller = Navigation.findNavController(v); controller.navigate(R.id.action_homeFragment_to_detailFragment); } }); return binding.getRoot(); //return inflater.inflate(R.layout.fragment_home, container, false); }}
xml
3、Fragment 副頁(yè)
package com.example.naviation01;import android.os.Bundle;import androidx.databinding.DataBindingUtil;import androidx.fragment.app.Fragment;import androidx.lifecycle.ViewModelProviders;import androidx.navigation.NavController;import androidx.navigation.Navigation;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.example.naviation01.databinding.FragmentDetailBinding;/** * A simple {@link Fragment} subclass. */public class DetailFragment extends Fragment { public DetailFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment MyViewMode myViewMode; myViewMode = ViewModelProviders.of(getActivity()).get(MyViewMode.class); FragmentDetailBinding binding; binding = DataBindingUtil.inflate(inflater,R.layout.fragment_detail,container,false); binding.setDate(myViewMode); binding.setLifecycleOwner(getActivity()); binding.button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NavController controller = Navigation.findNavController(v); controller.navigate(R.id.action_detailFragment_to_homeFragment); } }); return binding.getRoot(); //return inflater.inflate(R.layout.fragment_detail, container, false); }}
xml
4、xml Main_Activity
以上是“Android如何通過(guò)ViewModel保存數(shù)據(jù)實(shí)現(xiàn)多頁(yè)面的數(shù)據(jù)共享功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!