创建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来对数据进行生命周期的管理根据业务展开需求