其他相关文章:
android之LifeCycle:https://blog.csdn.net/li6472/article/details/119795952?spm=1001.2014.3001.5501
android之LiveData:https://blog.csdn.net/li6472/article/details/119784791?spm=1001.2014.3001.5501
android之MVVM框架使用之xml详解:https://blog.csdn.net/li6472/article/details/119782825?spm=1001.2014.3001.5501
android之MVVM框架使用之BaseObservable与双向绑定MVVM框架使用之BaseObservable与双向绑定:https://blog.csdn.net/li6472/article/details/119782825?spm=1001.2014.3001.5501
android之MVVM理解:https://blog.csdn.net/li6472/article/details/119762190?spm=1001.2014.3001.5501
amdroid之MVVM写一个简单的程序:https://blog.csdn.net/li6472/article/details/119761806?spm=1001.2014.3001.5501
android之ViewModel:ViewModel速成_li6472的博客-CSDN博客
使用BaseObservable可能出现的问题:Default interface methods are only supported starting with Android N (--min-api 24):_li6472的博客-CSDN博客
说明一下 :我利用了架构组件Livedata将Fragments储存,大家可以自由扩展,至于不懂得可以访问我的主页基本都有详解
一 编写Activity的XML
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<data>
<import type="android.view.View"/>
<variable
name="handlers"
type="thubdersoft.rising.star.mymap.MainActivity"/>
<variable
name="data"
type="String"/>
<variable
name="BlankVieModel"
type="viewmodel.BlankFragmentViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btnMVVM"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{handlers.onClickLoadData}"
android:text="点击请求数据"/>
<Button
android:id="@+id/btnToast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{handlers.onClickShowToastName}"
android:text="点击请求数据并Toast提示"/>
<TextView
android:id="@+id/tvData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@{userViewModel.data}"/>
<TextView
android:text="@{user.name}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tx_1"/>
<TextView
android:layout_below="@+id/tx_1"
android:text="@{String.valueOf(user.age)}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_show"
android:onClick="@{handlers.onClickShowFragment}"
android:text="shoeFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/blank_fl"
android:padding="10dp"
android:background="@color/blue"
android:visibility="@{BlankVieModel.getmBlnkFrangMentVisible?View.VISIBLE:View.GONE}"
tools:visibility="invisible"
android:layout_width="300dp"
android:layout_height="400dp"/>
</LinearLayout>
</layout>
大家在写XML文件时要一边XML写一边调用的文件,以达到路劲相符
比如每一个variable的name可以自由定义但type就要写入路径了
另外onClick也要调用相应父程序的方法
同理FrameLayout也是一样
二 写Fragment的XML
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="fhandles"
type="fragment.BlankFragment" />
<variable
name="eText"
type="fragment.BlankFragment" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/btn_fm_back"
android:text="返回"
android:onClick="@{fhandles.onClickBack}"
android:layout_width="100dp"
android:layout_height="50dp"/>
</LinearLayout>
</layout>
三 写?Fragment的View层
public class BlankFragment extends Fragment {
private static final String TAG = BlankFragment.class.getSimpleName();
private BlankfragmentBinding mBlankfragmentBinding;
private BlankFragmentViewModel mBlankFragmentViewModel;
public static BlankFragment newInstance(){
Bundle args = new Bundle();
BlankFragment blankFragment = new BlankFragment();
blankFragment.setArguments(args);
return blankFragment;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mBlankfragmentBinding = DataBindingUtil.inflate(inflater,
R.layout.blankfragment,container,false);
mBlankfragmentBinding.setLifecycleOwner(this);
mBlankFragmentViewModel = new BlankFragmentViewModel();
return mBlankfragmentBinding.getRoot();
}
public void onClickBack(View view){
}
}
初始化BlankFragment
把BlankFragment装进Bundle然后装进BlankfragmentBinding以实现将XML文件拿进来
下面的Fragment的一般写法
四 fragment的View Model层
public class BlankFragmentViewModel extends BaseObservable
implements ViewModeLoadDataCallBack {
private MutableLiveData<Boolean>mBlnkFrangMentVisible = new MutableLiveData<>();
public MutableLiveData<Boolean>getmBlnkFrangMentVisible(){
return mBlnkFrangMentVisible;
}
public void setBlankFragmentVisible(Boolean isVisible){
mBlnkFrangMentVisible.postValue(isVisible);
}
public BlankFragmentViewModel(){
super();
mBlnkFrangMentVisible.setValue(true);
}
@Override
public void onSuccess() {
}
@Override
public void onFailure() {
}
@Override
public void repalce() {
}
@Override
public void back(Boolean isBack) {
}
}
?在使用BaseObservable可能会发生APi错误主页有解决方式
这里呢就是将众多的FRagment装进Livedata然后设置Activity中的Framlayout的可见性
setValue属于Livedata的内容见主页Livedata速成
这里写了一个回调接口(有一个方法需要回调,其他的没用)
public interface ViewModeLoadDataCallBack {
public void onSuccess();
public void onFailure();
public void repalce();
public void back(Boolean isBack);
}
五 Activity的View Model层(也可不用,但要稍微修改Activity的代码)
public class ViewModel extends BaseObservable implements ViewModeLoadDataCallBack {
private Model mModel;
public ViewModel(){
mModel = new Model();
}
@Bindable
public String getData(){
return mModel.mData;
}
public void loadUserData(){
mModel.requestData(this);
}
@Override
public void onSuccess() {
notifyPropertyChanged(BR.data);
}
@Override
public void onFailure() {
}
@Override
public void repalce() {
}
@Override
public void back(Boolean isBack) {
}
}
这里就是最先开始的那两个按钮的状态和作用
六 Activity的Model层
public class Model {
public String mData;
public Model(){
this.mData = "初始数据";
}
public void requestData(ViewModeLoadDataCallBack mViewModeLoadDataCallBack){
this.mData = "数据请求成功";
mViewModeLoadDataCallBack.onSuccess();
}
}
这里就是设置最初那两个按钮的text
七 Activity的View层
public class MainActivity extends AppCompatActivity {
private ViewModel userViewModel;
private TextView tvData;
private ActivityMainBinding mMainBinding;
private BlankFragment mBlankFragment;
private Button mButtonShow;
private final List<Fragment>mFragments = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
mMainBinding.setUser(new UserBean("张三",33));
userViewModel = new ViewModel();
mMainBinding.setBlankVieModel(new BlankFragmentViewModel());
mMainBinding.setUserViewModel(userViewModel);
mMainBinding.setHandlers(this);
tvData = mMainBinding.tvData;
initFragments();
}
private void initFragments() {
mFragments.add(BlankFragment.newInstance());
Log.i("A","addBlankFragment");
}
public void onClickShowToastName(View view){
Toast.makeText(this,tvData.getText().toString(),Toast.LENGTH_LONG).show();
}
public void onClickLoadData(View view){
userViewModel.onSuccess();
}
public void onClickShowFragment(View view){
Log.i("A","onClickmButtonShow");
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.blank_fl,new BlankFragment()).commit();
}
}
熟悉了MvvM和Livedata后代码很简单没什么解释的
不熟悉一定要先搞懂原理?
|