forked from liangfeidotme/MasteringAndroidDataBinding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConversionsActivity.java
64 lines (52 loc) · 1.95 KB
/
ConversionsActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.liangfeizc.databinding.sample.converter;
import android.databinding.BindingAdapter;
import android.databinding.BindingConversion;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableBoolean;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import com.liangfeizc.databinding.R;
import com.liangfeizc.databinding.sample.BaseActivity;
import com.liangfeizc.databinding.databinding.ActivityConversionsBinding;
import com.liangfeizc.databinding.utils.ScreenUtils;
public class ConversionsActivity extends BaseActivity {
private ObservableBoolean mIsError = new ObservableBoolean();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityConversionsBinding binding =
DataBindingUtil.setContentView(this, R.layout.activity_conversions);
mIsError.set(true);
binding.setIsError(mIsError);
binding.setHeight(ScreenUtils.dp2px(this, 200));
}
public void toggleIsError(View view) {
mIsError.set(!mIsError.get());
}
@BindingConversion
public static ColorDrawable convertColorToDrawable(int color) {
return new ColorDrawable(color);
}
@BindingAdapter("layout_height")
public static void setLayoutHeight(View view, float height) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = (int) height;
view.setLayoutParams(params);
}
/** !!! Binding conversion should be forbidden, otherwise it will conflict with
* {@code android:visiblity} attribute.
*/
/*
@BindingConversion
public static int convertColorToString(int color) {
switch (color) {
case Color.RED:
return R.string.red;
case Color.WHITE:
return R.string.white;
}
return R.string.app_name;
}*/
}