package com.camnter.mvvm.view;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Description：MVVMFragment
 * Created by：CaMnter
 */

public abstract class MVVMFragment extends Fragment {

    protected View self;
    protected Activity activity;
    protected LayoutInflater inflater;

    protected ViewDataBinding rootBinding;


    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.activity = this.getActivity();
    }


    @Nullable @Override public View onCreateView(LayoutInflater inflater,
                                                 @Nullable ViewGroup container,
                                                 @Nullable Bundle savedInstanceState) {
        this.onBeforeDataBinding(savedInstanceState);
        final int layoutId = this.getLayoutId();
        if (layoutId == 0) return null;
        try {
            this.inflater = inflater;
            this.rootBinding = DataBindingUtil.inflate(inflater, layoutId, container, false);
            this.self = this.rootBinding.getRoot();
            // binding success, but maybe this.contentViewBinding == null
            this.onCastingRootBinding(this.rootBinding);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // cast success
        this.baseFragmentInit();
        this.onAfterDataBinding(savedInstanceState);
        // self may be null
        return this.self;
    }


    /**
     * Fill in layout id
     *
     * @return layout id
     */
    protected abstract int getLayoutId();

    /**
     * on casting root binding
     *
     * @param rootBinding rootBinding
     */
    protected abstract void onCastingRootBinding(
        @Nullable final ViewDataBinding rootBinding);


    /**
     * on before data binding
     *
     * @param savedInstanceState savedInstanceState
     */
    protected void onBeforeDataBinding(@Nullable final Bundle savedInstanceState) {
        // Nothing to do
    }


    /**
     * baseFragment init
     */
    protected void baseFragmentInit() {
        // Nothing to do
    }


    /**
     * on after data binding
     *
     * @param savedInstanceState savedInstanceState
     */
    protected abstract void onAfterDataBinding(@Nullable final Bundle savedInstanceState);

}

@@ -518,16 +518,17 @@
 tivity;%0A
+%0A
     prot
@@ -557,16 +557,51 @@
 nflater;
+%0A    protected ViewGroup container;
 %0A%0A    pr
@@ -644,32 +644,36 @@
 %0A%0A%0A    @Override
+%0A   
  public void onC
@@ -826,16 +826,20 @@
 Nullable
+%0A   
  @Overri
@@ -840,16 +840,20 @@
 Override
+%0A   
  public 
@@ -924,36 +924,16 @@
         
-                
-    
 @Nullabl
@@ -955,36 +955,16 @@
 tainer,%0A
-                    
         
@@ -1214,24 +1214,70 @@
 = inflater;%0A
+            if (this.autoInflateView()) %7B%0A    
             
@@ -1374,47 +1374,9 @@
     
-this.self = this.rootBinding.getRoot();
+%7D
 %0A   
@@ -1503,16 +1503,130 @@
 nding);%0A
+            if (this.rootBinding != null) %7B%0A                this.self = this.rootBinding.getRoot();%0A            %7D%0A
         
@@ -1826,24 +1826,24 @@
 may be null%0A
-
         retu
@@ -1856,32 +1856,168 @@
 s.self;%0A    %7D%0A%0A%0A
+    /**%0A     * default true%0A     *%0A     * @return auto ?%0A     */%0A    protected boolean autoInflateView() %7B%0A        return true;%0A    %7D%0A%0A%0A
     /**%0A     * F

add container in MVVMFragment of widget module