package com.aberigle.android.sliderview;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewDebug;
import android.widget.HorizontalScrollView;
import android.widget.TextView;

/**
 * Created by aberigle on 06/04/15.
 */
public class SlidingTabLayout extends HorizontalScrollView {

    private static final float TAB_VIEW_PADDING_DIPS = 16;

    private final SlidingTabStrip strip;

    private final int selectedTextColor;
    private final int textColor;

    private ViewPager           viewpager;
    private PagerChangeListener pagerListener;


    public SlidingTabLayout(Context context) { this(context, null); }

    public SlidingTabLayout(Context context, AttributeSet attributes) {
        super(context, attributes);
        setHorizontalScrollBarEnabled(false);
        setFillViewport(true);

        selectedTextColor = context.getResources().getColor(R.color.white);
        textColor = context.getResources().getColor(R.color.bright_foreground_dark_disabled);

        strip = new SlidingTabStrip(context, Color.WHITE);

        addView(strip);

    }

    private void populateFromPager() {
        PagerAdapter adapter = viewpager.getAdapter();
        int          padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);

        TextView tab;

        if (adapter != null) for (int i = 0; i < adapter.getCount(); i++) {
            tab = new TextView(getContext());
            final int index = i;
            tab.setGravity(Gravity.CENTER);
            tab.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
            tab.setTypeface(Typeface.DEFAULT_BOLD);
            tab.setTextColor(textColor);
            tab.setAllCaps(true);
            tab.setText(adapter.getPageTitle(i));
            tab.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    viewpager.setCurrentItem(index);
                }
            });
            tab.setPadding(padding,padding,padding,padding);
            strip.addView(tab);
        }

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (viewpager != null) {
            pagerListener.onPageSelected(viewpager.getCurrentItem());
            scrollToTab(viewpager.getCurrentItem(), 0);
        }
    }


    private int resolveAttr(int attrId) {
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(attrId, outValue, true);
        return outValue.data;
    }

    private void scrollToTab(int index, float positionOffset) {
        final int childCount = strip.getChildCount();
        if (childCount == 0 || index < 0 || index >= childCount) return;

        View selectedChild = strip.getChildAt(index);
        if (selectedChild != null) {
            int left = selectedChild.getLeft();

            if (index < (childCount - 1)) {
                View nextTitle = strip.getChildAt(index + 1);
                left = (int) (positionOffset * nextTitle.getLeft() + (1.0f - positionOffset) * left);
            }

            int targetScrollX = left + (selectedChild.getWidth() / 2) - (getWidth() / 2);
            scrollTo(targetScrollX, 0);
        }
    }

    public void setViewpager(ViewPager pager) {
        viewpager = pager;

        if (pager != null) pager.setOnPageChangeListener(pagerListener = new PagerChangeListener());

        populateFromPager();
    }

    private class PagerChangeListener implements ViewPager.OnPageChangeListener {

        private TextView oldPos;

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            strip.onViewPagerChanged(position, positionOffset);
            scrollToTab(position, positionOffset);
        }

        @Override
        public void onPageSelected(int position) {
            TextView selected = (TextView) strip.getChildAt(position);
            if (oldPos != null) if (!oldPos.equals(selected)) oldPos.setTextColor(textColor);
            oldPos = selected;
            selected.setTextColor(selectedTextColor);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

}

@@ -198,24 +198,67 @@
 gerAdapter;%0A
+import android.support.v4.view.ViewCompat;%0A
 import andro
@@ -283,24 +283,24 @@
 .ViewPager;%0A
-
 import andro
@@ -412,39 +412,8 @@
 ew;%0A
-import android.view.ViewDebug;%0A
 impo
@@ -858,16 +858,81 @@
 stener;%0A
+    private ViewPager.OnPageChangeListener onPageChangeListener;%0A
 %0A%0A    pu
@@ -1437,32 +1437,254 @@
 strip);%0A%0A    %7D%0A%0A
+    @Override%0A    public void setElevation(float elevation) %7B%0A        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.LOLLIPOP) super.setElevation(elevation);%0A        else ViewCompat.setElevation(this, elevation);%0A    %7D%0A%0A
     private void
@@ -4039,32 +4039,192 @@
 Pager();%0A    %7D%0A%0A
+    public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) %7B%0A        this.onPageChangeListener = onPageChangeListener;%0A    %7D%0A%0A
     private clas
@@ -4548,32 +4548,129 @@
 ositionOffset);%0A
+            onPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);%0A
         %7D%0A%0A     
@@ -4975,32 +4975,91 @@
 ctedTextColor);%0A
+            onPageChangeListener.onPageSelected(position);%0A
         %7D%0A%0A     
@@ -5129,16 +5129,81 @@
 tate) %7B%0A
+            onPageChangeListener.onPageScrollStateChanged(state);
 %0A       

add support for elevation and to register a OnPageChangeListener