Razhan
12/20/2018 - 10:05 AM

My DataBindingHelper for Android.

My DataBindingHelper for Android.

package your.project.view.helper;

import android.databinding.BindingAdapter;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.google.android.flexbox.FlexboxLayout;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;

import java.util.List;

import your.project.R;

public class DataBindingHelper {
    @BindingAdapter("cellClickable")
    public static void setCellClickable(View view, boolean clickable) {
        view.setClickable(clickable);
    }

    @BindingAdapter("cellBackground")
    public static void setSessionCellBackground(View view, @DrawableRes int backgroundResId) {
        view.setBackgroundResource(backgroundResId);
    }

    @BindingAdapter("webViewUrl")
    public static void loadUrl(WebView webView, String url) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        webView.loadUrl(url);
    }

    @BindingAdapter("webViewClient")
    public static void setWebViewClient(WebView webView, WebViewClient client) {
        webView.setWebViewClient(client);
    }

    @BindingAdapter("android:layout_height")
    public static void setLayoutHeight(View view, float height) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = (int) height;
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("android:layout_marginTop")
    public static void setTopMargin(View view, float margin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin, Math.round(margin), layoutParams.rightMargin, layoutParams.bottomMargin);
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("android:layout_marginBottom")
    public static void setBottomMargin(View view, float margin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin, layoutParams.rightMargin, Math.round(margin));
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("android:layout_marginLeft")
    public static void setLeftMargin(View view, float margin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(Math.round(margin), layoutParams.topMargin, layoutParams.rightMargin, layoutParams.bottomMargin);
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("android:layout_marginRight")
    public static void setRightMargin(View view, float margin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin, Math.round(margin), layoutParams.bottomMargin);
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("imageUrl")
    public static void setImageUrl(ImageView imageView, @Nullable String imageUrl) {
        setImageUrl(imageView, imageUrl, ContextCompat.getDrawable(imageView.getContext(), R.color.placeholder));
    }

    @BindingAdapter({"imageUrl", "imagePlaceHolder"})
    public static void setImageUrl(ImageView imageView, @Nullable String imageUrl, Drawable placeHolder) {
        if (TextUtils.isEmpty(imageUrl)) {
            Picasso.with(imageView.getContext()).cancelRequest(imageView);
            imageView.setImageDrawable(placeHolder);
        } else {
            RequestCreator requestCreator = Picasso.with(imageView.getContext())
                    .load(imageUrl);
            requestCreator.placeholder(placeHolder);
            requestCreator.error(placeHolder);

            boolean fitted = false;
            // if ImageView's width and height is wrap_content, Picasso can not fit.
            if (imageView.getLayoutParams().width != ViewGroup.LayoutParams.WRAP_CONTENT ||
                    imageView.getLayoutParams().height != ViewGroup.LayoutParams.WRAP_CONTENT) {
                requestCreator.fit();
                fitted = true;
            }
            // centerInside and centerCrop method is effective, if fited.
            if (fitted && imageView.getScaleType() == ImageView.ScaleType.CENTER_INSIDE) {
                requestCreator.centerInside();
            } else if (fitted && imageView.getScaleType() == ImageView.ScaleType.CENTER_CROP) {
                requestCreator.centerCrop();
            }
            requestCreator.into(imageView);
        }
    }

    @BindingAdapter("colorScheme")
    public static void setColorSchemeColors(SwipeRefreshLayout layout, @ColorInt int color) {
        layout.setColorSchemeColors(color);
    }

    @BindingAdapter("childViews")
    public static void childViews(ViewGroup layout, List<View> views) {
        layout.removeAllViews();
        for (View view : views) {
            layout.addView(view);
        }
        // force re-calculating the layout
        layout.requestLayout();
        layout.invalidate();
    }

    @BindingAdapter({"childViews", "viewPercent"})
    public static void childViews(FlexboxLayout layout, List<View> views, float viewPercent) {
        layout.removeAllViews();
        for (View view : views) {
            layout.addView(view);

            // For child views dynamically added to FlexboxLayout,
            // since the horizontal width can not be specified from the XML,
            // the size of the horizontal width is forcibly specified.
            FlexboxLayout.LayoutParams lp = (FlexboxLayout.LayoutParams) view.getLayoutParams();
            lp.setFlexBasisPercent(viewPercent);
        }
        // force re-calculating the layout
        layout.requestLayout();
        layout.invalidate();
    }
}