package com.matthewlogan.pushertabstrip.lib;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class PusherTabStrip extends RelativeLayout implements ViewPager.OnPageChangeListener {

    private Context mContext;

    private ViewPager mViewPager;

    private TextView[] mTextViews;

    private int mCurrentPosition;
    private int mCurrentOffsetPixels;

    private boolean mDidInitialLayout;

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

    public PusherTabStrip(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PusherTabStrip(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mContext = context;

        setWillNotDraw(false);
    }

    public void setViewPager(ViewPager viewPager) {
        mViewPager = viewPager;
        mViewPager.setOnPageChangeListener(this);

        mTextViews = new TextView[mViewPager.getAdapter().getCount()];

        for (int i = 0; i < mTextViews.length; i++) {
            TextView textView = new TextView(mContext);

            textView.setText(mViewPager.getAdapter().getPageTitle(i));

            addView(textView);

            LayoutParams lp =
                    (LayoutParams) textView.getLayoutParams();
            if (lp != null) {
                lp.addRule(CENTER_VERTICAL);
                textView.setLayoutParams(lp);
            }

            mTextViews[i] = textView;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        mCurrentPosition = position;
        mCurrentOffsetPixels = positionOffsetPixels;

        if (mDidInitialLayout) {
            layoutTextViews();
        } else {
            initialLayoutTextViews();
        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void initialLayoutTextViews() {
        for (int i = 0; i < mTextViews.length; i++) {
            if (i < mCurrentPosition - 1) {
                mTextViews[i].setX(-mTextViews[i].getMeasuredWidth());

            } else if (i == mCurrentPosition - 1) {
                mTextViews[i].setX(0.f);

            } else if (i == mCurrentPosition) {
                mTextViews[i].setX(getWidth() / 2.f - mTextViews[i].getMeasuredWidth() / 2.f);

            } else if (i == mCurrentPosition + 1) {
                mTextViews[i].setX(getWidth() - mTextViews[i].getMeasuredWidth());

            } else if (i > mCurrentPosition) {
                mTextViews[i].setX(getWidth());
            }
        }

        mDidInitialLayout = true;
    }

    private void layoutTextViews() {
        TextView curTextView = mTextViews[mCurrentPosition];

        float center = getWidth() / 2.f - curTextView.getMeasuredWidth() / 2.f;
        float curX = center - mCurrentOffsetPixels;
        if (curX < 0) {
            curX = 0;
        }

        curTextView.setX(curX);

        if (mCurrentPosition > 0) {
            TextView prevTextView = mTextViews[mCurrentPosition - 1];

            float prevX = 0.f;
            if (curX <= prevTextView.getWidth()) {
                prevX = curX - prevTextView.getWidth();
            }

            prevTextView.setX(prevX);
        }

        if (mCurrentPosition < mTextViews.length - 1) {
            TextView nextTextView = mTextViews[mCurrentPosition + 1];

            float nextRight = getWidth() - nextTextView.getMeasuredWidth();
            float nextCenter = getWidth() / 2.f - nextTextView.getMeasuredWidth() / 2.f;
            float distanceToMove = nextRight - nextCenter;

            float nextX = nextRight;
            if (getWidth() - mCurrentOffsetPixels <= distanceToMove) {
                nextX = nextRight - (distanceToMove - (getWidth() - mCurrentOffsetPixels));
            }

            nextTextView.setX(nextX);

            if (mCurrentPosition < mTextViews.length - 2) {
                TextView nextNextTextView = mTextViews[mCurrentPosition + 2];

                float nextNextX = nextX + nextTextView.getMeasuredWidth();
                if (nextNextX < getWidth() - nextNextTextView.getMeasuredWidth()) {
                    nextNextX = getWidth() - nextNextTextView.getMeasuredWidth();
                }

                nextNextTextView.setX(nextNextX);
            }
        }
    }
}

@@ -147,16 +147,42 @@
 uteSet;%0A
+import android.view.View;%0A
 import a
@@ -337,16 +337,38 @@
 Listener
+, View.OnClickListener
  %7B%0A%0A    
@@ -1217,32 +1217,38 @@
 ) %7B%0A            
+final 
 TextView textVie
@@ -1344,24 +1344,72 @@
 Title(i));%0A%0A
+            textView.setOnClickListener(this);%0A%0A
             
@@ -2185,32 +2185,260 @@
 tate) %7B%0A%0A    %7D%0A%0A
+    @Override%0A    public void onClick(View v) %7B%0A        for (int i = 0; i %3C mTextViews.length; i++) %7B%0A            if (v.equals(mTextViews%5Bi%5D)) %7B%0A                mViewPager.setCurrentItem(i, true);%0A            %7D%0A        %7D%0A    %7D%0A%0A
     private void

Make the textviews clickable