写使用java一个MVVM的Android项目,包含一个activity和两个fragment,使用Fragment + LiveData + DataBinding+Lifecycle
时间: 2024-01-25 15:02:52 浏览: 37
好的,下面是一个简单的示例:
首先,在build.gradle文件中添加以下依赖项:
```groovy
// ViewModel and LiveData
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
// Data Binding
implementation 'androidx.databinding:databinding-runtime:4.0.1'
```
接下来,创建一个名为MainActivity的Activity,并在其布局文件中添加两个Fragment的占位符:
activity_main.xml:
```xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</layout>
```
MainActivity.java:
```java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
// 加载第一个Fragment
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new FirstFragment())
.commit();
}
}
```
接下来,创建一个名为FirstFragment的Fragment,并在其布局文件中使用DataBinding绑定数据:
first_fragment.xml:
```xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{viewModel.text}" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:onClick="@{viewModel::onNextClicked}" />
</LinearLayout>
</layout>
```
FirstFragment.java:
```java
public class FirstFragment extends Fragment {
private FirstViewModel viewModel;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
// 使用DataBinding绑定布局文件
FirstFragmentBinding binding = DataBindingUtil.inflate(inflater, R.layout.first_fragment, container, false);
// 创建ViewModel实例
viewModel = ViewModelProviders.of(this).get(FirstViewModel.class);
// 将ViewModel与布局文件中的变量绑定
binding.setViewModel(viewModel);
// 设置LifecycleOwner,以便LiveData知道何时更新UI
binding.setLifecycleOwner(this);
return binding.getRoot();
}
}
```
下面是FirstViewModel.java:
```java
public class FirstViewModel extends ViewModel {
private MutableLiveData<String> textLiveData = new MutableLiveData<>();
public FirstViewModel() {
// 初始化LiveData的默认值
textLiveData.setValue("Hello, World!");
}
public LiveData<String> getText() {
return textLiveData;
}
public void onNextClicked() {
// 更新LiveData的值
textLiveData.setValue("Next Clicked!");
}
}
```
最后,创建另一个名为SecondFragment的Fragment,并在MainActivity中添加一个方法来加载它:
second_fragment.xml:
```xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{viewModel.text}" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previous"
android:onClick="@{viewModel::onPreviousClicked}" />
</LinearLayout>
</layout>
```
SecondFragment.java:
```java
public class SecondFragment extends Fragment {
private SecondViewModel viewModel;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
// 使用DataBinding绑定布局文件
SecondFragmentBinding binding = DataBindingUtil.inflate(inflater, R.layout.second_fragment, container, false);
// 创建ViewModel实例
viewModel = ViewModelProviders.of(this).get(SecondViewModel.class);
// 将ViewModel与布局文件中的变量绑定
binding.setViewModel(viewModel);
// 设置LifecycleOwner,以便LiveData知道何时更新UI
binding.setLifecycleOwner(this);
return binding.getRoot();
}
}
```
下面是SecondViewModel.java:
```java
public class SecondViewModel extends ViewModel {
private MutableLiveData<String> textLiveData = new MutableLiveData<>();
public SecondViewModel() {
// 初始化LiveData的默认值
textLiveData.setValue("Goodbye, World!");
}
public LiveData<String> getText() {
return textLiveData;
}
public void onPreviousClicked() {
// 更新LiveData的值
textLiveData.setValue("Previous Clicked!");
}
}
```
最后,在MainActivity中添加一个方法来加载SecondFragment:
```java
private void loadSecondFragment() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new SecondFragment())
.commit();
}
```
现在,你的MVVM Android项目就完成了!你可以使用LiveData和ViewModel来管理数据,并使用DataBinding将数据绑定到UI上。Lifecycle组件可确保UI在活动和片段之间正确地进行管理。