通過ViewModel實現(xiàn)的數(shù)據(jù)共享符合Android的MVC設(shè)計模式,將數(shù)據(jù)獨立出來
成都創(chuàng)新互聯(lián)主要從事成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)湯旺,10余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
實現(xiàn)的Demo
1、主頁面通過SeekBar 來改變數(shù)字的值
2、點擊進入就進入第二個界面,但是數(shù)據(jù)還是共享的
3、隨便加兩個數(shù)字上去,再次切換
4、發(fā)現(xiàn)數(shù)據(jù)還是共享的
下面是具體實現(xiàn)步驟:
1、建立兩個Fragment(使用了Binding 和 Navigation)
一點要添加Binding 和 Navigation 不然做不了
2、建立一個繼承于ViewModel的類
3、分別在兩個Fragment的代碼中使用繼承于ViewModel的那個類,就可以實現(xiàn)數(shù)據(jù)共享
下面是具體代碼:
1、繼承于ViewModel的類
package com.example.naviation01; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; public class MyViewMode extends ViewModel { private MutableLiveDatanumber; public MutableLiveData getNumber(){ if(this.number == null){ this.number = new MutableLiveData<>(); this.number.setValue(0); } return this.number; } public void add(int x){ this.number.setValue(this.number.getValue()+x); if(this.number.getValue() < 0){ this.number.setValue(0); } } }
2、Fragment 主頁
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
<?xml version="1.0" encoding="utf-8"?>
3、Fragment 副頁
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
<?xml version="1.0" encoding="utf-8"?>
4、xml Main_Activity
<?xml version="1.0" encoding="utf-8"?>
總結(jié)
以上所述是小編給大家介紹的Android通過ViewModel保存數(shù)據(jù)實現(xiàn)多頁面的數(shù)據(jù)共享功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!