You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: todoapp/README.md
+34-35Lines changed: 34 additions & 35 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
# TODO-DataBinding
2
2
3
3
It is based on the [todo-mvp](https://github.com/googlesamples/android-architecture/tree/todo-mvp/todoapp) sample and uses the Data Binding library (currently in beta) to display data
4
-
and bind UI elements to actions.
4
+
and bind UI elements to actions.
5
5
6
6
It's doesn't follow a strict Model-View-ViewModel or a Model-View-Presenter
7
7
pattern, as it uses both View Models and Presenters.
@@ -20,52 +20,51 @@ data model.
20
20
In the todo-mvp sample, a Task description is set in the [TaskDetailFragment](https://github.com/googlesamples/android-architecture/blob/todo-mvp/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailFragment.java):
21
21
22
22
23
-
```
24
-
public void onCreateView(...) {
25
-
...
26
-
mDetailDescription = (TextView)
23
+
```java
24
+
publicvoid onCreateView(...) {
25
+
...
26
+
mDetailDescription = (TextView)
27
27
root.findViewById(R.id.task_detail_description);
28
-
}
28
+
}
29
29
30
-
@Override
31
-
public void showDescription(String description) {
32
-
mDetailDescription.setVisibility(View.VISIBLE);
33
-
mDetailDescription.setText(description);
34
-
}
30
+
@Override
31
+
publicvoid showDescription(String description) {
32
+
mDetailDescription.setVisibility(View.VISIBLE);
33
+
mDetailDescription.setText(description);
34
+
}
35
35
```
36
36
In this sample, the [TaskDetailFragment](https://github.com/googlesamples/android-architecture/blob/todo-databinding/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailFragment.java) simply passes the Task to the data binding:
37
37
38
38
39
-
```
40
-
@Override
41
-
public void showTask(Task task) {
42
-
mViewDataBinding.setTask(task);
43
-
}
39
+
```java
40
+
@Override
41
+
publicvoid showTask(Task task) {
42
+
mViewDataBinding.setTask(task);
43
+
}
44
44
```
45
45
and the library will take care of displaying it, as defined by the layout (<code>[taskdetail\_frag.xml](https://github.com/googlesamples/android-architecture/blob/todo-databinding/todoapp/app/src/main/res/layout/taskdetail_frag.xml)</code>)
46
46
47
47
48
-
```
49
-
<TextView
50
-
android:id="@+id/task_detail_description"
51
-
...
52
-
android:text="@{task.description}" />
53
-
48
+
```xml
49
+
<TextView
50
+
android:id="@+id/task_detail_description"
51
+
...
52
+
android:text="@{task.description}" />
54
53
```
55
54
### Event binding
56
55
57
-
Data binding eliminates the need to call <code>findViewById() </code>and event binding can also help minimizing <code>setOnClickListener()</code>.
56
+
Data binding eliminates the need to call <code>findViewById() </code>and event binding can also help minimizing <code>setOnClickListener()</code>.
58
57
59
58
In this CheckBox from <code>[taskdetail\_frag.xml](https://github.com/googlesamples/android-architecture/blob/todo-databinding/todoapp/app/src/main/res/layout/taskdetail_frag.xml)</code>, the presenter is called directly when the user taps on it:
60
59
61
60
62
-
```
63
-
<CheckBox
61
+
```xml
62
+
<CheckBox
64
63
android:id="@+id/task_detail_complete"
65
64
...
66
65
android:checked="@{task.completed}"
67
66
android:onCheckedChanged="@{(cb, isChecked) ->
68
-
presenter.completeChanged(task, isChecked)}" />
67
+
presenter.completeChanged(task, isChecked)}" />
69
68
```
70
69
### Observing data
71
70
@@ -75,15 +74,15 @@ relevant properties are notified and the UI elements bound to those properties
0 commit comments