Share
Embed
[Roman Herasymenko] Deal with it: Android DataBinding explained
Presentation from GDG DevFest Ukraine 2016.
Learn more at: https://devfest.gdg.org.ua
Google Developers Group Lviv
September 10, 2016
Transcript
36 slides
-
#dfua Model-View-Controller Controller layer - (Activity/Fragment), aware both of the
View and Model Model layer - same as the Controller layer View - XML layout only -
#dfua Model-View-Presenter DataManager - model layer, holds a reference to
Retrofit, Database, etc. Presenter Layer - in the “middle” between View layer and Model Layer, doesn’t know anything about Activity/Fragment/View View Layer - combination of Activity/Fragment with implementation of the View interface and XML layout. Holds soft reference to Presenter. -
#dfua Model-View-ViewModel XML Activity/Fragment ViewModel REST Api Database DataManager POJO
Request For updates DataBinding Model layer -
#dfua What is the difference? Controller - replaced by ViewModel,
and located below UI layer ViewModel - exposes the data and command objects for the View ViewModel - receives its data from the Model layer Model - business and validation -
#dfua build.gradle apply plugin: 'com.android.application' android { dataBinding {
enabled = true } } Setup -
#dfua fragment_user.xml <layout> <data class=“UserBinding”> <variable name="data" type="co.yamert.binding.UserViewModel" /> </data>
<!-- the rest of your original layout here --> </layout> View -
#dfua fragment_user.xml <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TextInputEditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="match_parent"
bind:addTextChangedListener="@{viewModel.usernameWatcher}" /> </android.support.design.widget.TextInputLayout> View -
#dfua public class UserViewModel extends BaseObservable { private UserInteractor
interactor; private String username; public UserViewModel(UserInteractor interactor, long id) { this.interactor = interactor; if (id != WRONG_ID) loadUserById(id); } public final TextWatcher usernameWatcher = new TextWatcherAdapter() { @Override public void afterTextChanged(Editable s) { username = TextUtils.isEmpty(s)? “” : s.toString(); notifyPropertyChanged(co.yamert.binding.BR.username); } }; @Bindable public String getUsername() { return username; } } ViewModel -
#dfua public class UserFragment extends Fragment implements UserInteractor {
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) { UserBinding binding = DataBindingUtil.inflate(R.layout.fragment_user); UserViewModel vm = new UserViewModel(this, id); binding.setViewModel(vm); return binding.getRoot(); } @Override public void onDialogButton() { Intent intent = new Intent(); startActivity(intent); } } Bring it all together -
#dfua <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TextInputEditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="match_parent" android:text=“@{data.username}“/>
</android.support.design.widget.TextInputLayout> One-Way Binding -
#dfua <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TextInputEditText android:layout_width="match_parent" android:layout_height="match_parent" android:text=“@={data.username}“/>
</android.support.design.widget.TextInputLayout> Two-Way Binding -
#dfua <layout> <LinearLayout> <TextView android:id="@+id/hello" /> <include android:id="@+id/included"
layout="@layout/included_layout" /> </LinearLayout> </layout> Include -
#dfua public class ColorPicker extends View { private int color;
public ColorPicker(Context context) { super(context); } public void setColor(int color) { this.color = color; invalidate(); } public void addListener(OnColorChangeListener listener) { } public void removeListener(OnColorChangeListener listener) { } } Custom setter -
#dfua <co.yamert.binding.views.ColorPicker android:layout_width="match_parent" android:layout_height="match_parent" app:color=“@{color}" /> public void setColor(int
color) { this.color = color; invalidate(); } Custom setter -
#dfua public class BindingAdapters { @BindingAdapter("color") public static void
setPickerColor(ColorPicker picker, int color) { if (picker != null) picker.setColor(color); } } Binding adapter as setter -
#dfua @BindingAdapter("onColorChange") public static void setColorChangeListener(ColorPicker view, OnColorChangeListener oldListener, OnColorChangeListener
newListener) { if (oldListener != null) view.removeListener(oldListener); if (newListener != null) view.addListener(newListener); } Events handling -
#dfua @BindingAdapter({"bind:font"}) public static void setFont(TextView textView, String fontName) {
textView.setTypeface(Typeface.createFromAsset(textView .getContext() .getAssets(), "fonts/" + fontName)); } <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:font="@{'Roboto-Regular.ttf'}" /> Tip: Fonts binding -
#dfua @BindingAdapter(value = {"imageUrl", "placeholder"}, requireAll = false) public static
void setImageUrl(ImageView imageView, String url, Drawable placeHolder) { if (url == null) { imageView.setImageDrawable(placeholder); } else { ImageLoader.loadInto(imageView, url, placeholder); } } Binding adapter -
#dfua @BindingAdapter("usersList") public static void setUsersList(RecyclerView recycler, List<User> users) {
if (recycler.getAdapter() != null) return; Context context = recycler.getContext(); LinearLayoutManager manager = new LinearLayoutManager(context); manager.setOrientation(LinearLayoutManager.HORIZONTAL); recycler.setLayoutManager(manager); recycler.setHasFixedSize(true); recycler.setAdapter(new UserAdapter(users)); } Binding adapter -
#dfua <View android:background="@{isError ? @color/red : @color/white}" android:layout_width="wrap_content" android:layout_height=“wrap_content"/>
@BindingConversion public static ColorDrawable convertColorToDrawable(int color) { return new ColorDrawable(color); } Binding conversion