package com.datdo.mobilib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.datdo.mobilib.R;
import com.datdo.mobilib.util.MblUtils;

/**
 * Created by datdvt on 2015/05/28.
 */
public class MblYieldLayout extends FrameLayout {

    public MblYieldLayout(Context context) {
        super(context);
    }

    public MblYieldLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MblYieldLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MblYieldLayout, 0, 0);
        final int yieldId = ta.getResourceId(R.styleable.MblYieldLayout_yieldId, -1);
        final int layoutId = ta.getResourceId(R.styleable.MblYieldLayout_layoutId, -1);

        if (yieldId < 0 || layoutId < 0) {
            return;
        }

        MblUtils.getMainThreadHandler().post(new Runnable() {
            @Override
            public void run() {

                if (getChildCount() == 0) {
                    return;
                }

                View child = getChildAt(0);
                View layout = MblUtils.getLayoutInflater().inflate(layoutId, null);
                ViewGroup yieldLayout = (ViewGroup) layout.findViewById(yieldId);

                removeView(child);
                yieldLayout.addView(child);
                layout.setLayoutParams(getLayoutParams());
                addView(layout);
            }
        });
    }
}

@@ -212,21 +212,22 @@
 .widget.
-Frame
+Linear
 Layout;%0A
@@ -380,13 +380,14 @@
 nds 
-Frame
+Linear
 Layo
@@ -401,77 +401,55 @@
    p
-ublic MblYieldLayout(Context context) %7B%0A        super(context);%0A    %7D
+rivate int mYieldId;%0A    private int mLayoutId;
 %0A%0A  
@@ -583,173 +583,8 @@
  %7D%0A%0A
-    public MblYieldLayout(Context context, AttributeSet attrs, int defStyleAttr) %7B%0A        super(context, attrs, defStyleAttr);%0A        init(context, attrs);%0A    %7D%0A%0A
     
@@ -636,25 +636,24 @@
 et attrs) %7B%0A
-%0A
         Type
@@ -760,19 +760,10 @@
     
-final int y
+mY
 ield
@@ -837,19 +837,10 @@
     
-final int l
+mL
 ayou
@@ -904,17 +904,16 @@
 d, -1);%0A
-%0A
         
@@ -916,17 +916,18 @@
     if (
-y
+mY
 ieldId %3C
@@ -932,17 +932,18 @@
  %3C 0 %7C%7C 
-l
+mL
 ayoutId 
@@ -965,152 +965,173 @@
     
-return;%0A        %7D%0A%0A        MblUtils.getMainThreadHandler().post(new Runnable() %7B%0A            @Override%0A            public void run
+throw new RuntimeException(MblYieldLayout.class + %22: layoutId or yieldId is missing%22);%0A        %7D%0A    %7D%0A%0A    @Override%0A    protected void onFinishInflate
 () %7B%0A
-%0A
         
     
@@ -1126,28 +1126,77 @@
         
-        if (
+super.onFinishInflate();%0A%0A        View%5B%5D children = new View%5B
 getChild
@@ -1206,16 +1206,10 @@
 nt()
- == 0) %7B
+%5D;
 %0A   
@@ -1217,73 +1217,75 @@
     
-            return;%0A                %7D%0A%0A                View child
+for (int i = 0; i %3C getChildCount(); i++) %7B%0A            children%5Bi%5D
  = g
@@ -1294,17 +1294,17 @@
 ChildAt(
-0
+i
 );%0A     
@@ -1298,32 +1298,34 @@
 dAt(i);%0A        
+%7D%0A
         View lay
@@ -1367,17 +1367,18 @@
 inflate(
-l
+mL
 ayoutId,
@@ -1385,32 +1385,151 @@
  null);%0A        
+if (layout == null) %7B%0A            throw new RuntimeException(MblYieldLayout.class + %22: layoutId not found%22);%0A        %7D%0A
         ViewGrou
@@ -1576,17 +1576,18 @@
 iewById(
-y
+mY
 ieldId);
@@ -1587,25 +1587,24 @@
 eldId);%0A
-%0A
         
         
@@ -1599,32 +1599,118 @@
     
-        removeView(child
+if (yieldLayout == null) %7B%0A            throw new RuntimeException(MblYieldLayout.class + %22: yieldId not found%22
 );%0A 
@@ -1716,24 +1716,27 @@
         
+%7D%0A%0A
         
 yieldLay
@@ -1731,92 +1731,106 @@
     
-yieldLayout.addView(child);%0A                layout.setLayoutParams(getLayoutParams()
+for (View child : children) %7B%0A            removeView(child);%0A            yieldLayout.addView(child
 );%0A 
@@ -1832,24 +1832,26 @@
 d);%0A        
+%7D%0A
         addV
@@ -1867,34 +1867,8 @@
 t);%0A
-            %7D%0A        %7D);%0A
     

Improve MblYieldLayout