package com.ywl5320.navigationbar.bar;
import android.app.Activity;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by ywl on 2016/6/27.
*/
public class NavitationLayout extends RelativeLayout{
private TextView[] textViews; // 标题栏数组,用于存储要显示的标题
private LinearLayout titleLayout; //标题栏父控件
private ViewPager viewPager;
private View bgLine; //导航背景色
private View navLine; //导航条颜色
private int navWidth = 0; //导航条宽度
private int txtUnselectedColor = 0;
private int txtSelectedColor = 0;
private int txtUnselectedSize = 16;
private int txtSelectedSize = 16;
private int widOffset = 0;
private OnTitleClickListener onTitleClickListener;
private OnNaPageChangeListener onNaPageChangeListener;
public NavitationLayout(Context context) {
this(context, null);
}
public NavitationLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public NavitationLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
titleLayout = new LinearLayout(context);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
titleLayout.setLayoutParams(layoutParams);
titleLayout.setOrientation(LinearLayout.HORIZONTAL);
addView(titleLayout);
}
public void setOnTitleClickListener(OnTitleClickListener onTitleClickListener) {
this.onTitleClickListener = onTitleClickListener;
}
public void setOnNaPageChangeListener(OnNaPageChangeListener onNaPageChangeListener) {
this.onNaPageChangeListener = onNaPageChangeListener;
}
private void setTitles(Context context, String[] titles, final boolean smoothScroll)
{
this.textViews = new TextView[titles.length];
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT);
params.weight = 1;
params.gravity = Gravity.CENTER;
// 循环,根据标题栏动态生成TextView来显示标题,每个标题栏的宽度比例为1:1,其中的内容居中。
for(int i = 0; i < titles.length; i++)
{
final int index = i;
TextView textView = new TextView(context);
textView.setText(titles[i]);
textView.setGravity(Gravity.CENTER);
textViews[i] = textView;
textViews[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(index, smoothScroll);
if(onTitleClickListener != null)
{
onTitleClickListener.onTitleClick(v);
}
}
});
titleLayout.addView(textView, params);
}
}
private void setTitles(Context context, String[] titles, final boolean smoothScroll, int splilinecolor, float splilinewidth, float topoffset, float bottomoffset)
{
this.textViews = new TextView[titles.length];
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT);
params.weight = 1;
params.gravity = Gravity.CENTER;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(dip2px(context, splilinewidth),LayoutParams.MATCH_PARENT);
lp.setMargins(0, dip2px(context, topoffset), 0, dip2px(context, bottomoffset));
// 循环,根据标题栏动态生成TextView来显示标题,每个标题栏的宽度比例为1:1,其中的内容居中。
for(int i = 0; i < titles.length; i++)
{
final int index = i;
TextView textView = new TextView(context);
textView.setText(titles[i]);
textView.setGravity(Gravity.CENTER);
textViews[i] = textView;
textViews[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(index, smoothScroll);
if(onTitleClickListener != null)
{
onTitleClickListener.onTitleClick(v);
}
}
});
titleLayout.addView(textView, params);
if(i < titles.length - 1)
{
View view = new View(context);
view.setBackgroundColor(splilinecolor);
titleLayout.addView(view, lp);
}
}
}
/**
* 设置导航背景色
* @param context
* @param height
* @param color
*/
public void setBgLine(Context context, int height, int color)
{
height = dip2px(context,height);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, height);
bgLine = new View(context);
bgLine.setLayoutParams(layoutParams);
bgLine.setBackgroundColor(context.getResources().getColor(color));
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, height);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
addView(bgLine, lp);
}
/**
* 设置导航条颜色
* @param context
* @param height
* @param color
* @param currentPosition
*/
public void setNavLine(Activity context, int height, int color, int currentPosition)
{
if(textViews != null)
{
navWidth = getScreenWidth(context) / textViews.length;
}
height = dip2px(context,height);
System.out.println("width:" + navWidth);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, height);
navLine = new View(context);
navLine.setLayoutParams(layoutParams);
navLine.setBackgroundColor(context.getResources().getColor(color));
LayoutParams lp = new LayoutParams(navWidth, height);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
addView(navLine, lp);
moveBar(navLine, navWidth, widOffset, currentPosition);
}
/**
*
* @param context 上下文
* @param titles 标题栏
* @param viewPager
* @param unselectedcolor 未选中字体颜色
* @param setectedcolor 选中字体颜色
* @param txtUnselectedSize 未选中字体大小
* @param txtSelectedSize 选中字体大小
* @param currentPosition 当前viewpager的位置
* @param widOffset 导航条的边距
* @param smoothScroll 滑动类型
*/
public void setViewPager(final Context context, String[] titles, ViewPager viewPager, final int unselectedcolor, final int setectedcolor, int txtUnselectedSize, final int txtSelectedSize, final int currentPosition, int widOffset, boolean smoothScroll)
{
this.viewPager = viewPager;
this.txtUnselectedColor = unselectedcolor;
this.txtSelectedColor = setectedcolor;
this.txtUnselectedSize = txtUnselectedSize;
this.txtSelectedSize = txtSelectedSize;
this.widOffset = dip2px(context, widOffset);
viewPager.setCurrentItem(currentPosition);
setTitles(context, titles, smoothScroll);
setUnselectedTxtColor(context, unselectedcolor, txtUnselectedSize);
setSelectedTxtColor(context, setectedcolor, txtSelectedSize, currentPosition);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {