沉浸式、MVP、MVVM

作者: kim_liu | 来源:发表于2018-08-09 13:25 被阅读7次

    1.沉浸式状态栏

    获取状态栏的高度,然后把toolbar的高度加上状态栏的高度,再设置状态栏透明
    设置状态栏透明:

    //沉浸式状态栏
           //4.4以上才有沉浸式状态栏的效果
           if(Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT){
               //设置状态栏为透明的
               getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
               //获取状态栏的高度
               final int statusBarHeight = (int) KimliuUtils.getStatusBarHeight(getResources());
               //获取到控件宽高
               status_bar.post(new Runnable() {
                   @Override
                   public void run() {
                       int height = status_bar.getHeight();
                       LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) status_bar.getLayoutParams();
                       layoutParams.height = height + statusBarHeight;
                       status_bar.setLayoutParams(layoutParams);
                   }
               });
           }
    

    获取状态栏高度

     ////////////////////获取状态栏高度
        public static float getStatusBarHeight(Resources resources){
            int status_bar_height_id = resources.getIdentifier("status_bar_height", "dimen", "android");
            return resources.getDimension(status_bar_height_id);
        }
    

    2.MVP

    M:Mode
    V:View 负责UI逻辑(如弹出吐司等)
    P:Presenter 负责业务逻辑

    2.1

    每一个页面都有自己的UI逻辑,因此每个页面都需要有一个自己的View接口,这些View接口又继承自一个总的View接口:BaseView。比如:mainActivity需要一个MainView接口,其中有showToast等逻辑,OtherActivity需要一个OtherView接口,其中有showToast,showProgress等逻辑。这两个View接口中都有showToast,那么可以把这个方法抽取到BaseView中,继承BaseView即可。

    2.2

    每一个页面也都有自己的业务逻辑,因此每一个页面都需要一个自己的presenter接口,这些presenter都有自己的实现类,并且都继承自basepresenter。比如mainActivity需要实现登录的逻辑,那么mainPresenter中就需要login();在mainPresenterImpl中去实现login方法。
    BasePresenter中有所有的presenter都需要实现的方法,attachView
    和deattachView。

    3.MVVM

    使用Data Binding库
    The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.
    查看官方文档:
    https://developer.android.google.cn/topic/libraries/data-binding/
    3.1 在项目中使用Data Binding

    android {
        ...
        dataBinding {
            enabled = true
        }
    }
    

    3.2 修改布局

    <?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">
    
        <data>
            <variable
                name="user"
                type="com.kimliu.huaweimarket.test.mvvm.mode.User"/>
        </data>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="20sp"
            android:text="@{user.name}"/>
    </LinearLayout>
    </layout>
    

    3.3 绑定数据

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           //ActivityMvvmTestBinding 这个类的名字是根据后面的layout名称自动生成的
            ActivityMvvmTestBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_mvvm_test);
            User user = new User();
            user.name = "zhangsan";
            binding.setUser(user);
        }
    

    3.4 动态更新name的值
    使用ObservableField 将实体类user的name属性改成

     public final ObservableField<String> firstName = new ObservableField<>();
    

    3.5 给控件绑定事件
    1.在实体类中添加一个方法

    public void showName(View view){
            KimliuUtils.showToast(name.get());
        }
    

    2.在xml中调用该方法

     <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="20sp"
            android:text="@{user.name}"
            android:onClick="@{user.showName}"/>
    

    3.5 使用BindingAdapter更改控件上显示的数据
    1.xml中

     <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="20sp"
            android:text="@{user.name}"
            android:onClick="@{user.showName}"
            app:newName="@{user.newName}"/>
    

    2.在实体类中创建方法

    //添加标签  并且()中的内容要和xml中的app:后面的字符串一致
     @BindingAdapter({"newName"})
        public static void setNewName(TextView tv,ObservableField<String> newName){
            tv.setText(newName.get());
        }
    

    相关文章

      网友评论

        本文标题:沉浸式、MVP、MVVM

        本文链接:https://www.haomeiwen.com/subject/irijeftx.html