Created
August 18, 2015 14:18
-
-
Save Sirelon/9613c1f254996859703b to your computer and use it in GitHub Desktop.
Rounded tabs for the ViewPager. It is custom view, which show specific rouneded tabs in viewPager. All for you needed it is declare this View in your xml file and in code after youe setted adapter for ViewPager call method setViewPager in RoundedTabs object. Later i will add possibility to add custom colors and size with xml attributes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sirelon; | |
import android.content.Context; | |
import android.content.res.ColorStateList; | |
import android.graphics.Color; | |
import android.graphics.drawable.Drawable; | |
import android.graphics.drawable.GradientDrawable; | |
import android.graphics.drawable.StateListDrawable; | |
import android.support.v4.view.PagerAdapter; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.Gravity; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
/** | |
* @author romanishin | |
* @since 17.08.15. | |
*/ | |
public class RoundedTabs extends LinearLayout { | |
private ViewPager mViewPager; | |
private static final float RADIUS = 25; | |
private static final int TEXT_PADDING = (int) RADIUS; | |
private static final int SEL_COLOR = Color.argb(255, 86, 86, 86); | |
private static final int NOR_COLOR = Color.argb(255, 255, 255, 255); | |
private static final int STROKE_COLOR = SEL_COLOR; | |
private static final int STROKE_WIDHT = (int) (RADIUS / 4); | |
public RoundedTabs(Context context) { | |
super(context); | |
init(); | |
} | |
public RoundedTabs(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public RoundedTabs(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
private void init() { | |
setOrientation(HORIZONTAL); | |
} | |
public void setViewPager(ViewPager viewPager) { | |
if (viewPager.getAdapter() == null) | |
throw new NullPointerException("Before calling this method, be sure, that your viewpager with the adapter!"); | |
mViewPager = viewPager; | |
int viewCount = mViewPager.getAdapter().getCount(); | |
removeAllViews(); | |
createViews(viewCount, mViewPager.getAdapter()); | |
int item = mViewPager.getCurrentItem(); | |
selectItem(item); | |
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { | |
@Override | |
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { | |
} | |
@Override | |
public void onPageSelected(int position) { | |
selectItem(position); | |
} | |
@Override | |
public void onPageScrollStateChanged(int state) { | |
} | |
}); | |
} | |
private View prevView; | |
protected void selectItem(int item) { | |
if (prevView != null) | |
prevView.setSelected(false); | |
prevView = getChildAt(item); | |
prevView.setSelected(true); | |
} | |
private void createViews(int viewCount, PagerAdapter adapter) { | |
for (int i = viewCount - 1; i >= 0; --i) { | |
CharSequence title = adapter.getPageTitle(i); | |
TextView textView; | |
if (i == 0) | |
textView = createFirstView(title); | |
else if (i == viewCount - 1) | |
textView = createLastView(title); | |
else | |
textView = createMiddleView(title); | |
final int position = i; | |
textView.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
mViewPager.setCurrentItem(position, true); | |
} | |
}); | |
} | |
} | |
private TextView createFirstView(CharSequence title) { | |
float[] radii = getCorners(true, RADIUS); | |
TextView textView = createTextView(title, radii); | |
addView(textView, 0); | |
return textView; | |
} | |
private TextView createMiddleView(CharSequence title) { | |
float[] radii = getCorners(true, 0); | |
TextView textView = createTextView(title, radii); | |
addView(textView); | |
return textView; | |
} | |
private TextView createLastView(CharSequence title) { | |
float[] radii = getCorners(false, RADIUS); | |
TextView textView = createTextView(title, radii); | |
addView(textView, getChildCount() - 1); | |
return textView; | |
} | |
private TextView createTextView(CharSequence title, float[] radii) { | |
TextView textView = new TextView(getContext()); | |
textView.setText(title); | |
int padding = (int) (10 / getResources().getDisplayMetrics().density); | |
textView.setPadding(padding, padding, padding, padding); | |
textView.setGravity(Gravity.CENTER); | |
textView.setTextColor(createTextColors()); | |
LayoutParams layout_params = new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1); | |
textView.setLayoutParams(layout_params); | |
textView.setPadding(TEXT_PADDING, TEXT_PADDING, TEXT_PADDING, TEXT_PADDING); | |
textView.setClickable(true); | |
textView.setBackgroundDrawable(createBackground(radii)); | |
return textView; | |
} | |
private ColorStateList createTextColors() { | |
int[][] states = new int[][]{ | |
new int[]{android.R.attr.state_pressed}, | |
new int[]{android.R.attr.state_selected}, | |
new int[]{} | |
}; | |
int[] colors = new int[]{ | |
NOR_COLOR, | |
NOR_COLOR, | |
SEL_COLOR | |
}; | |
return new ColorStateList(states, colors); | |
} | |
private Drawable createBackground(float[] radii) { | |
StateListDrawable listDrawable = new StateListDrawable(); | |
listDrawable.addState(new int[]{android.R.attr.state_pressed}, createSingleBackground(SEL_COLOR, radii)); | |
listDrawable.addState(new int[]{android.R.attr.state_selected}, createSingleBackground(SEL_COLOR, radii)); | |
listDrawable.addState(new int[]{}, createSingleBackground(NOR_COLOR, radii)); | |
return listDrawable; | |
} | |
private Drawable createSingleBackground(int argb, float[] radii) { | |
GradientDrawable drawable = new GradientDrawable(); | |
drawable.setColor(argb); | |
drawable.setCornerRadii(radii); | |
drawable.setStroke(STROKE_WIDHT, STROKE_COLOR); | |
return drawable; | |
} | |
protected float[] getCorners(boolean isLeft, float radius) { | |
float[] floats = new float[8]; | |
int index = isLeft ? 0 : 1; | |
for (int i = 0; i < 2; ++i) { | |
int idx1 = index * 2 + i; | |
int idx2 = 8 - 1 - idx1; | |
floats[idx1] = radius; | |
floats[idx2] = radius; | |
} | |
return floats; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment