package im.quar.autolayout.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.ViewGroup;

import im.quar.autolayout.AutoLayoutInfo;
import im.quar.autolayout.utils.AutoLayoutHelper;


/**
 * Created by DTHeaven on 15/12/9.
 */
public class AutoRecyclerView extends RecyclerView {
    private final AutoLayoutHelper mHelper = new AutoLayoutHelper(this);

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

    public AutoRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!isInEditMode())
            mHelper.adjustChildren();
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public static class LayoutParams extends RecyclerView.LayoutParams
            implements AutoLayoutHelper.AutoLayoutParams {
        private AutoLayoutInfo mAutoLayoutInfo;

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            mAutoLayoutInfo = AutoLayoutHelper.getAutoLayoutInfo(c, attrs);
            mAutoLayoutInfo.fillAttrs(this);
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }

        public LayoutParams(ViewGroup.MarginLayoutParams source) {
            super(source);
        }

        @Override
        public AutoLayoutInfo getAutoLayoutInfo() {
            return mAutoLayoutInfo;
        }


    }
}

@@ -136,28 +136,141 @@
 .widget.
-RecyclerView
+GridLayoutManager;%0Aimport android.support.v7.widget.RecyclerView;%0Aimport android.support.v7.widget.StaggeredGridLayoutManager
 ;%0Aimport
@@ -942,32 +942,45 @@
 ride%0A    public 
+RecyclerView.
 LayoutParams gen
@@ -1011,32 +1011,366 @@
 uteSet attrs) %7B%0A
+        LayoutManager layoutManager = getLayoutManager();%0A        if (layoutManager instanceof GridLayoutManager) %7B%0A            return new GridLayoutParams(getContext(), attrs);%0A        %7D else if (layoutManager instanceof StaggeredGridLayoutManager) %7B%0A            return new StaggeredGridLayoutParams(getContext(), attrs);%0A        %7D%0A%0A
         return n
@@ -2460,16 +2460,1974 @@
      %7D%0A%0A
+    %7D%0A%0A    public static class GridLayoutParams extends GridLayoutManager.LayoutParams%0A            implements AutoLayoutHelper.AutoLayoutParams %7B%0A%0A        private AutoLayoutInfo mAutoLayoutInfo;%0A%0A        public GridLayoutParams(Context c, AttributeSet attrs) %7B%0A            super(c, attrs);%0A            mAutoLayoutInfo = AutoLayoutHelper.getAutoLayoutInfo(c, attrs);%0A            mAutoLayoutInfo.fillAttrs(this);%0A        %7D%0A%0A        public GridLayoutParams(int width, int height) %7B%0A            super(width, height);%0A        %7D%0A%0A        public GridLayoutParams(MarginLayoutParams source) %7B%0A            super(source);%0A        %7D%0A%0A        public GridLayoutParams(ViewGroup.LayoutParams source) %7B%0A            super(source);%0A        %7D%0A%0A        public GridLayoutParams(RecyclerView.LayoutParams source) %7B%0A            super(source);%0A        %7D%0A%0A        @Override%0A        public AutoLayoutInfo getAutoLayoutInfo() %7B%0A            return mAutoLayoutInfo;%0A        %7D%0A    %7D%0A%0A    public static class StaggeredGridLayoutParams extends StaggeredGridLayoutManager.LayoutParams%0A            implements AutoLayoutHelper.AutoLayoutParams %7B%0A%0A        private AutoLayoutInfo mAutoLayoutInfo;%0A%0A        public StaggeredGridLayoutParams(Context c, AttributeSet attrs) %7B%0A            super(c, attrs);%0A            mAutoLayoutInfo = AutoLayoutHelper.getAutoLayoutInfo(c, attrs);%0A            mAutoLayoutInfo.fillAttrs(this);%0A        %7D%0A%0A        public StaggeredGridLayoutParams(int width, int height) %7B%0A            super(width, height);%0A        %7D%0A%0A        public StaggeredGridLayoutParams(MarginLayoutParams source) %7B%0A            super(source);%0A        %7D%0A%0A        public StaggeredGridLayoutParams(ViewGroup.LayoutParams source) %7B%0A            super(source);%0A        %7D%0A%0A        public StaggeredGridLayoutParams(RecyclerView.LayoutParams source) %7B%0A            super(source);%0A        %7D%0A%0A        @Override%0A        public AutoLayoutInfo getAutoLayoutInfo() %7B%0A            return mAutoLayoutInfo;%0A        %7D
 %0A    %7D%0A%7D

Fix AutoRecyclerView bug when using StaggeredGridLayoutManager.