官方地址
https://developer.android.google.cn/topic/libraries/architecture/viewmodel
使用ViewModel fragment间共享数据
1、创建ViewModel
package com.zqq.h5shell.activity.jectpack_navigation.model;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.zqq.h5shell.activity.jectpack_navigation.bean.User;
public class ShareViewModel extends ViewModel {
private final MutableLiveData<User> user=new MutableLiveData<>();
public void setUser(User u){
user.setValue(u);
}
public MutableLiveData<User> getUser() {
return user;
}
}
2、创建fragmen1、fragment2
package com.zqq.h5shell.activity.jectpack_navigation.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModelProvider;
import com.zqq.h5shell.R;
import com.zqq.h5shell.activity.jectpack_navigation.bean.User;
import com.zqq.h5shell.activity.jectpack_navigation.model.ShareViewModel;
import com.zqq.h5shell.databinding.FragmentShare1Binding;
public class Share1Fragment extends Fragment {
private ShareViewModel shareViewModel;
private FragmentShare1Binding binding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_share1, container, false);
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
shareViewModel = new ViewModelProvider(requireActivity()).get(ShareViewModel.class);
MutableLiveData<User> user = shareViewModel.getUser();
User value = user.getValue();
binding.setUser(value);
//点击按钮更新viewmodel数据
binding.btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
User newUser = new User("share1", "share1", 1660);
user.postValue(newUser);
binding.setUser(newUser);
binding.notifyChange();
}
});
}
}
布局如下,使用DataBinding
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="user"
type="com.zqq.h5shell.activity.jectpack_navigation.bean.User" />
</data>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.toString()}"
/>
<Button
android:id="@+id/btn_change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改为share1"
/>
</LinearLayout>
</layout>
fragment2同1
package com.zqq.h5shell.activity.jectpack_navigation.fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModelProvider;
import com.zqq.h5shell.R;
import com.zqq.h5shell.activity.jectpack_navigation.bean.User;
import com.zqq.h5shell.activity.jectpack_navigation.model.ShareViewModel;
import com.zqq.h5shell.databinding.FragmentShare2Binding;
public class Share2Fragment extends Fragment {
private ShareViewModel shareViewModel;
private FragmentShare2Binding binding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_share2, container, false);
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
shareViewModel = new ViewModelProvider(requireActivity()).get(ShareViewModel.class);
MutableLiveData<User> user = shareViewModel.getUser();
User value = user.getValue();
binding.setUser(value);
binding.btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
User newUser=new User("share2","share2",180);
user.postValue(newUser);
binding.setUser(newUser);
binding.notifyChange();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="user"
type="com.zqq.h5shell.activity.jectpack_navigation.bean.User" />
</data>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.toString()}"
/>
<Button
android:id="@+id/btn_change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改为share2"
/>
</LinearLayout>
</layout>
3、activity如下,使用navigation
package com.zqq.h5shell.activity.jectpack_navigation;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.NavHostController;
import androidx.navigation.fragment.NavHostFragment;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.zqq.h5shell.ARouterContent;
import com.zqq.h5shell.R;
import com.zqq.h5shell.activity.jectpack_navigation.bean.User;
import com.zqq.h5shell.activity.jectpack_navigation.model.ShareViewModel;
import com.zqq.h5shell.databinding.ActivityShareviewmodelBinding;
@Route( path = ARouterContent.ShareViewModelActivity)
public class ShareViewModelActivity extends AppCompatActivity {
private ActivityShareviewmodelBinding binding;
private NavController navController;
private ShareViewModel shareViewModel;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_shareviewmodel);
initData();
initView();
}
private void initData() {
shareViewModel = new ViewModelProvider(this).get(ShareViewModel.class);
MutableLiveData<User> user = shareViewModel.getUser();
User userBean=new User("activity","activity",100);
user.setValue(userBean);
}
private void initView() {
Menu menu = binding.btmNavView.getMenu();
menu.add(0,0,0,"首页");
menu.getItem(0).setIcon(R.drawable.home);
menu.add(0,1,1,"我的");
menu.getItem(1).setIcon(R.drawable.me);
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_share);
navController = navHostFragment.getNavController();
binding.btmNavView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
CharSequence title = menuItem.getTitle();
if("首页".equals(title)){
navController.navigate(R.id.share1Fragment);
}else if("我的".equals(title)){
navController.navigate(R.id.share2Fragment);
}
return true;
}
});
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/navigation_share"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toTopOf="@id/btm_nav_view"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/navigation_share"
app:defaultNavHost="false"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/btm_nav_view"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_48"
app:layout_constraintTop_toBottomOf="@id/navigation_share"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:itemTextColor="@color/bootom_selector"
app:itemIconTint="@color/bootom_selector"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
navigation_shar
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/navigation_share"
app:startDestination="@id/share1Fragment">
<fragment
android:id="@+id/share1Fragment"
android:name="com.zqq.h5shell.activity.jectpack_navigation.fragment.Share1Fragment"
android:label="Share1Fragment" />
<fragment
android:id="@+id/share2Fragment"
android:name="com.zqq.h5shell.activity.jectpack_navigation.fragment.Share2Fragment"
android:label="Share2Fragment" />
</navigation>
实现效果:
- 默认显示 “activity…”
- 在Share1Fragment点击修改之后,显示为"share1…",切换fragment到Share2Fragment显示为"share1…"
- Share2Fragment点击修改会变化为"share2…"
-