MVVM模式下ObservableField - Drawable 和 ObservableField- Boolean 运用

1、布局中引入SettingViewModel,并在图片的布局AppCompatImageView中采用 app:imageResourceDrawable="@{viewModel.oneIcon}"  和 TextView 文本 android:text="@{viewModel.dropBoxSign ? @string/setting_cloud_sign_out : @string/setting_cloud_sign_in }"

<?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="viewModel"
            type="cn.wps.pdf.document.settings.SettingViewModel"/>
    </data>

    <LinearLayout
        android:paddingLeft="@dimen/user_info_pid_margin_size"
        android:paddingRight="@dimen/user_info_pid_margin_size"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <View
            style="@style/line_style_h"
            android:layout_marginLeft="@dimen/pdf_setting_main_padding_l_r"
            android:layout_marginStart="@dimen/pdf_setting_main_padding_l_r" />
        <LinearLayout
            android:onClick="@{viewModel.onOneDriveSign}"
            android:id="@+id/one_drive_sign_out"
            android:gravity="center_vertical"
            android:layout_width="match_parent"
            android:layout_height="@dimen/home_about_item_height"
            android:orientation="horizontal">

            <TextView
                android:layout_weight="1"
                android:textColor="@color/text_color"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="OneDrive" />


            <android.support.v7.widget.AppCompatImageView
                android:id="@+id/one_drive_icon_sign"
                app:imageResourceDrawable="@{viewModel.oneIcon}"
                android:layout_width="30dp"
                android:layout_height="wrap_content" />

        </LinearLayout>
        <View
            style="@style/line_style_h"
            android:layout_marginLeft="@dimen/pdf_setting_main_padding_l_r"
            android:layout_marginStart="@dimen/pdf_setting_main_padding_l_r" />
<RelativeLayout
    android:id="@+id/drop_dox_sign_layout"
    android:layout_width="match_parent"
    android:layout_height="66dp"
    android:background="@drawable/touch_bg_rectangle"
    android:onClick="@{viewModel.onDropBoxSignClick}"
    android:orientation="vertical"
    android:paddingTop="@dimen/pdf_setting_main_padding_t_b"
    android:paddingBottom="@dimen/pdf_setting_main_padding_t_b">

    <TextView
        android:id="@+id/drop_dox_sign"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="16dp"
        android:text="@{viewModel.dropBoxSign ? @string/setting_cloud_sign_out : @string/setting_cloud_sign_in }"
        android:textColor="#ff2291f9"
        android:textSize="13sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="@string/public_documents_drop_box_title"
            android:textColor="@color/text_color"
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="374018652@qq.com"
            android:textColor="@color/public_theme_gray"
            android:textSize="12sp"
            android:visibility="gone" />
    </LinearLayout>
</RelativeLayout>
    </LinearLayout>
</layout>

2、SettingViewModel 中定义这两个类型的变量 并调用

public ObservableField<Drawable> oneIcon = new ObservableField<>();

oneIcon.set(getActivity().getResources().getDrawable(R.drawable.oauth_out));

oneIcon.set(getActivity().getResources().getDrawable(R.drawable.oauth_sign));

 

public ObservableField<Boolean> dropBoxSign = new ObservableField<>();

mViewModel.dropBoxSign.set(true);  // false

 

 

 

发布了4 篇原创文章 · 获赞 3 · 访问量 771
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览