创立ViewmMode工厂类来进行数据的处理,并向外暴露出ViewModel的接口


class BlankViewModel : ViewModel() {
/*创立MutableLiveData保存数据*/
   private val liveData=MutableLiveData<Int>()
  /*数据对象*/ 
   private var i=0
/*获取MutableLiveData数值方法*/
     fun getLivdata():MutableLiveData<Int>{
        return liveData
    }
    /*对MutableLiveData的操作方法*/
    fun addOne(){
        i++
        liveData.value=i
    }
}

创立Activity直接操作BlankViewModel()的addone()方法

class FragmentMainActivity: AppCompatActivity() {
  val viewModel:BlankViewModel by viewModels ()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.fragment_mst_activity)
    findViewById<Button>(R.id.bt_fragmentMain).apply {
        setOnClickListener {
            viewModel.addOne()
            Log.d(TAG, "onCreate:${viewModel.getLivdata().value}")
        }
    }
    }
}

一起创立二个Fragment的Xml布局,同享一个Activity的ViewMode的数据源(后续依据事务进行数据分隔)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ViewModel.Fragment.FragmentMainActivity">
    <Button
        android:id="@+id/bt_fragmentMain"
        android:layout_width="match_parent"
        android:layout_height="50dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <fragment
            android:tag="com.example.jetpackmode.ViewModel.Fragment.LeftFragment"
            android:name="com.example.jetpackmode.ViewModel.Fragment.LeftFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
        <fragment
            android:tag="com.example.jetpackmode.ViewModel.Fragment.RightFragment"
            android:name="com.example.jetpackmode.ViewModel.Fragment.RightFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

一起创立左右二个fragment的XMl布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ViewModel.Fragment.LeftFragment">
    <TextView
        android:id="@+id/tv_leftFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#2237AC"
        android:gravity="center"
        android:text="无数据" />
</FrameLayout>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ViewModel.Fragment.RightFragment">
    <TextView
        android:id="@+id/tv_rightFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#2237AC"
        android:gravity="center"
        android:text="无数据" />
</FrameLayout>

创立左右两头fragment的事务逻辑

class LeftFragment : Fragment() {
    val viewModel:BlankViewModel by activityViewModels()
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_left, container, false)
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.getLivdata().observe(viewLifecycleOwner){
           view.findViewById<TextView>(R.id.tv_leftFragment).apply {
               text=it.toString()
           }
        }
    }
}

以上是依据gradle7.2

implementation "androidx.activity:activity-ktx:1.5.1
implementation "androidx.fragment:fragment-ktx:1.5.1
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1

曾经的写法现已抛弃了
一起能够使用liveData来对数据进行生命周期的办理依据事务打开需求