最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android通過ViewModel保存數(shù)據(jù)實(shí)現(xiàn)多頁(yè)面的數(shù)據(jù)共享功能

 更新時(shí)間:2019年11月05日 11:27:47   作者:毛少雄  
這篇文章主要介紹了Android通過ViewModel保存數(shù)據(jù)實(shí)現(xiàn)多頁(yè)面的數(shù)據(jù)共享功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

通過ViewModel實(shí)現(xiàn)的數(shù)據(jù)共享符合Android的MVC設(shè)計(jì)模式,將數(shù)據(jù)獨(dú)立出來(lái)

實(shí)現(xiàn)的Demo

1、主頁(yè)面通過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<Integer> number;
  public MutableLiveData<Integer> 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 主頁(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

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools">
  <data>
    <variable
      name="data"
      type="com.example.naviation01.MyViewMode" />
  </data>
  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeFragment">
    <androidx.constraintlayout.widget.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@{String.valueOf(data.number)}"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.255" />
      <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.456" />
      <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/function01"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.679" />
    </androidx.constraintlayout.widget.ConstraintLayout>
  </FrameLayout>
</layout>

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

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools">
  <data>
    <variable
      name="date"
      type="com.example.naviation01.MyViewMode" />
  </data>
  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DetailFragment">
    <androidx.constraintlayout.widget.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <TextView
        android:id="@+id/textView3"
        android:layout_width="131dp"
        android:layout_height="55dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@{String.valueOf(date.number)}"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.23" />
      <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/function02"
        android:onClick="@{()->date.add(1)}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.104"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499" />
      <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/function03"
        android:onClick="@{()->date.add(-1)}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.899"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499" />
      <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/function04"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.664" />
    </androidx.constraintlayout.widget.ConstraintLayout>
  </FrameLayout>
</layout>

4、xml Main_Activity

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">
  <fragment
    android:id="@+id/fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>

總結(jié)

以上所述是小編給大家介紹的Android通過ViewModel保存數(shù)據(jù)實(shí)現(xiàn)多頁(yè)面的數(shù)據(jù)共享功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論

兴化市| 新田县| 明光市| 宣恩县| 霍城县| 中江县| 屏边| 武定县| 上饶县| 襄樊市| 兴义市| 遵义市| 志丹县| 崇信县| 灵台县| 隆回县| 南平市| 无锡市| 正镶白旗| 昔阳县| 曲阳县| 克拉玛依市| 涞源县| 达州市| 德清县| 新疆| 沂源县| 龙游县| 石楼县| 革吉县| 淅川县| 稷山县| 平顶山市| 博野县| 阜阳市| 潼关县| 永定县| 万源市| 香港| 波密县| 双江|