虽然有很多Indicator 库,但是会引入没必要的资源,所以就自己写了一个最简单的指示器,记录一下。
public class MyIndictor extends LinearLayout {
//绑定的 viewpager
private ViewPager mViewPager;
// viewpager 的适配器
private PagerAdapter mPagerAdapter;
// 添加到 视图中的 view 集合
private List<ViewGroup> mViewGroups = new ArrayList<>();
private ColorStateList mTextColor; //文字颜色
private ColorStateList mIndictorColor; //指示器颜色
private View lastSelectedIcon; //上一次 选中的 指示器
private View lastSelectedText; //上一次 选中行的 文字
public MyIndictor(Context context) {
this(context, null);
}
public MyIndictor(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MyIndictor(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
//读取 xml 中的配置
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyIndictor, defStyleAttr, 0);
//解析属性
mTextColor = a.getColorStateList(R.styleable.MyIndictor_Indictor_Text_Color);
mIndictorColor = a.getColorStateList(R.styleable.MyIndictor_Indictor_Color);
a.recycle();
//设置 对齐方式
this.setGravity(Gravity.CENTER_VERTICAL);
}
/**
* 与 viewpager 进行绑定
* @param viewPager
*/
public void setUpWithViewPager(ViewPager viewPager) {
this.mViewPager = viewPager;
this.mPagerAdapter = viewPager.getAdapter();
//设置 监听
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//修改选中样式
if (lastSelectedIcon != null) lastSelectedIcon.setSelected(false);
if (lastSelectedText != null) lastSelectedText.setSelected(false);
setSelect(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
int count = mPagerAdapter.getCount();
//循环创建 指示器
for (int i = 0; i < count; i++) {
CharSequence title = mPagerAdapter.getPageTitle(i);
LinearLayout linearlayout = createLinearlayout(i, title);
addView(linearlayout);
}
setSelect(0);
}
/**
* 根据 选中 index 设置样式
* @param position
*/
private void setSelect(int position) {
View textView = mViewGroups.get(position).getChildAt(0);
View view = mViewGroups.get(position).getChildAt(1);
textView.setSelected(true);
view.setSelected(true);
lastSelectedIcon = textView;
lastSelectedText = view;
}
/**
* 创建指示器
*/
private LinearLayout createLinearlayout(final int index, CharSequence text) {
LinearLayout linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.CENTER);
//创建上方文本 控件
TextView textView = new TextView(getContext());
textView.setTextColor(mTextColor);
textView.setTextSize(14);
textView.setText(text);
LinearLayout.LayoutParams textParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(textParams);
//触发主动 测量
textView.measure(0,0);
linearLayout.addView(textView);
//创建下方指示器控件
TextView view = new TextView(getContext());
view.setBackground(getSelector(getDrawable(mIndictorColor.getDefaultColor()), getDrawable(mIndictorColor.getColorForState(new int[]{android.R.attr.state_selected}, Color.RED))));
//宽度设置为 指示器的宽度
int width = textView.getMeasuredWidth();
LinearLayout.LayoutParams viewParams = new LayoutParams(width, UIUtils.dip2px(2));
viewParams.topMargin = UIUtils.dip2px(6);
view.setLayoutParams(viewParams);
linearLayout.addView(view);
//设置布局 属性
LinearLayout.LayoutParams params = new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
params.weight = 1;
linearLayout.setLayoutParams(params);
//点击事件 修改 viewpager 的选中
linearLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(index);
}
});
mViewGroups.add(linearLayout);
return linearLayout;
}
/**
* 创建 drawable
* @param solid
* @return
*/
public static GradientDrawable getDrawable(int solid) {
GradientDrawable gradientDrawable = new GradientDrawable();
//设置填充颜色
gradientDrawable.setColor(solid);
//设置形状
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setCornerRadius(UIUtils.dip2px(2));
return gradientDrawable;
}
/**
* 代码创建Selector多状态图形
* @param normalDrawable
* @param pressDrawable
* @return
*/
public static StateListDrawable getSelector(Drawable normalDrawable, Drawable pressDrawable) {
StateListDrawable stateListDrawable = new StateListDrawable();
//给当前的颜色选择器添加选中图片的指向状态,未选中图片的指向状态
stateListDrawable.addState(new int[]{android.R.attr.state_selected}, pressDrawable);
// stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, normalDrawable);
//设置默认状态
stateListDrawable.addState(new int[]{}, normalDrawable);
return stateListDrawable;
}
}