andorid view如何附带数据_Android通过ViewModel保存数据实现多页面的数据共享功能...

本文通过一个实例展示了如何在Android中利用ViewModel进行数据共享,适用于多页面间的数据持久。详细介绍了创建ViewModel类,以及在两个Fragment中使用该类进行数据交互的步骤,包括SeekBar的进度更改和按钮事件监听,确保数据在页面切换时依然保留。
摘要由CSDN通过智能技术生成

通过ViewModel实现的数据共享符合Android的MVC设计模式,将数据独立出来

实现的Demo

1、主页面通过SeekBar 来改变数字的值

2、点击进入就进入第二个界面,但是数据还是共享的

3、随便加两个数字上去,再次切换

4、发现数据还是共享的

下面是具体实现步骤:

1、建立两个Fragment(使用了Binding 和 Navigation)

一点要添加Binding 和 Navigation 不然做不了

2、建立一个继承于ViewModel的类

3、分别在两个Fragment的代码中使用继承于ViewModel的那个类,就可以实现数据共享

下面是具体代码:

1、继承于ViewModel的类

package com.example.naviation01;

import androidx.lifecycle.MutableLiveData;

import androidx.lifecycle.ViewModel;

public class MyViewMode extends ViewModel {

private MutableLiveData number;

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

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools">

name="data"

type="com.example.naviation01.MyViewMode" />

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".HomeFragment">

android:layout_width="match_parent"

android:layout_height="match_parent">

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" />

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" />

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" />

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

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools">

name="date"

type="com.example.naviation01.MyViewMode" />

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".DetailFragment">

android:layout_width="match_parent"

android:layout_height="match_parent">

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" />

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" />

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" />

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" />

4、xml Main_Activity

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">

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" />

总结

以上所述是小编给大家介绍的Android通过ViewModel保存数据实现多页面的数据共享功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值