欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

實現(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 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 主頁

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 副頁

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>

總結

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

相關文章

  • Gradle屬性設置及環(huán)境變量全面教程

    Gradle屬性設置及環(huán)境變量全面教程

    這篇文章主要為大家介紹了Gradle屬性設置及環(huán)境變量的全面教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • RecyclerView的使用之HelloWorld

    RecyclerView的使用之HelloWorld

    RecyclerView是伴隨Android 5.0發(fā)布的新控件,是一種列表容器,Google意在用新的RecyclerView來取代老舊的ListView和GridView,它的使用靈活性和性能都要優(yōu)于ListView,通過本文給大家介紹RecyclerView的使用之HelloWorld,需要的朋友參考下
    2016-03-03
  • Android內(nèi)置SQLite的使用詳細介紹

    Android內(nèi)置SQLite的使用詳細介紹

    這篇文章主要介紹了Android內(nèi)置SQLite的使用詳細介紹,文章通過文章展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Android中系統(tǒng)自帶鎖WalkLock與KeyguardLock用法實例詳解

    Android中系統(tǒng)自帶鎖WalkLock與KeyguardLock用法實例詳解

    這篇文章主要介紹了Android中系統(tǒng)自帶鎖WalkLock與KeyguardLock用法,結合實例形式較為詳細的分析了WalkLock與KeyguardLock的功能、作用、使用方法與相關注意事項,需要的朋友可以參考下
    2016-01-01
  • 一篇文章弄懂kotlin的擴展方法

    一篇文章弄懂kotlin的擴展方法

    這篇文章主要給大家介紹了如何通過一篇文章弄懂kotlin的擴展方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用kotlin具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-09-09
  • Android使用ContentResolver搜索手機通訊錄的方法

    Android使用ContentResolver搜索手機通訊錄的方法

    這篇文章主要介紹了Android使用ContentResolver搜索手機通訊錄的方法,結合實例形式分析了Android中ContentResolver操作手機通訊錄的具體步驟與相關實現(xiàn)技巧,需要的朋友可以參考下
    2016-01-01
  • 配置Android SDK

    配置Android SDK

    今天小編就為大家分享一篇關于配置Android SDK的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • Android手機鬧鐘服務AlarmManagerk開發(fā)案例

    Android手機鬧鐘服務AlarmManagerk開發(fā)案例

    這篇文章主要為大家詳細介紹了Android手機鬧鐘服務AlarmManagerk開發(fā)案例的資料,需要的朋友可以參考下
    2016-05-05
  • Android解析json數(shù)據(jù)示例代碼(三種方式)

    Android解析json數(shù)據(jù)示例代碼(三種方式)

    本篇文章主要介紹了Android解析json數(shù)據(jù)示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Android中的LeakCanary的原理詳解

    Android中的LeakCanary的原理詳解

    大家好,本篇文章主要講的是Android中的LeakCanary的原理詳解,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02

最新評論