**Android三五行代码结局有滑块,可滑动的TABLE导航栏。类似斗鱼直播的直播页面导航栏,滑块可动态根据栏目宽度适应,无需自定义view**先来说下思路把,很简单,一个HorizontalScrollView 嵌套两个布局文件 第一个作为名称,第二个作为table栏滑块,根据viewpager的偏移量 ,不断的改变滑块view 的marginleft。。就是这么简单,废话不说,上代码。布局文件是这样滴:啊 <?xml version="1.0" encoding="utf-8"?><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical" tools:context="com.example.mypplication.Main3Activity">
<HorizontalScrollView android:layout_width="match_parent" android:layout_height="48dp">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/top_ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"/>
<LinearLayout android:id="@+id/bottom_rl" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="wrap_content">
<TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="4dp" android:background="@color/new_app_text_color" android:text="TiaoMu0" android:textColor="@color/new_app_text_color"/>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout>接下来就是三五行的核心代码啦:
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) bottom_tv.getLayoutParams();
/** * pos(当前所在页面)和position(下一个页面)以及offset来 * 设置mTabLineIv的左边距 滑动场景: * 记3个页面, * 从左到右分别为0,1,2,3 * 0->1; 1->2; 2->1; 1->0 *///
if (positionOffsetPixels >= 0) {
float one = Utils.getTextViewLength((TextView) top_ll.getChildAt(0), seedTitles.get(position)); int two;
float f;
if (pos != position) {
pos = position;
// setSelectorUI(pos);
}
if (uiPos == pos && positionOffset > 0.5) {
uiPos += 1;
setSelectorUI(uiPos);
}
else if (uiPos > pos && positionOffset < 0.5) {
uiPos -= 1;
setSelectorUI(uiPos);
}
if (pos == position)// 0->1
{
f = positionOffset;
lp.leftMargin = (int) ((positionOffset * left.get(position)) + leftTotal.get(pos) +DisplayUtil.dip2px(context, 13));
if (left.get(pos) > left.get(pos + 1)) {
two = left.get(pos) - left.get(pos + 1);
one = one - (two * f);
} else if (left.get(pos) < left.get(pos + 1)) {
two = left.get(pos + 1) - left.get(pos);
one = one + (two * f);
}
}
// else if (pos - 1 == position)
// {
//
// f = (1 - positionOffset);
//lp.leftMargin = (int) ((-(1 - positionOffset) * left.get(position)+leftTotal.get(pos)+DisplayUtil.dip2px(context, 13)));
// if (left.get(pos) > left.get(pos - 1)) {
// two = left.get(pos) - left.get(pos - 1);
// one = one - (two * f);
// } else if (left.get(pos) < left.get(pos - 1)) {
// two = left.get(pos - 1) - left.get(pos);
// one = one + (two * f);
// }
// }
// else if (pos < position) {
// f = positionOffset;
// lp.leftMargin = (int) ((positionOffset * left.get(position)) + leftTotal.get(position) + DisplayUtil.dip2px(context, 13));
// if (left.get(position) > left.get(position + 1)) {
// two = left.get(position) - left.get(position + 1);
// one = one - (two * f);
// } else if (left.get(position) < left.get(position + 1)) {
// two = left.get(position + 1) - left.get(position);
// one = one + (two * f);
// }
//
// } else if(pos>position){
// f = (1 - positionOffset);
// lp.leftMargin = (int) ((-(1 - positionOffset) * left.get(position) + leftTotal.get(position) + DisplayUtil.dip2px(context, 13)));
// if (left.get(position) > left.get(position - 1)) {
// two = left.get(position) - left.get(position - 1);
// one = one - (two * f);
// } else if (left.get(position) < left.get(position - 1)) {
// two = left.get(position - 1) - left.get(position);
// one = one + (two * f);
// }
// } lp.width = (int) one; bottom_tv.setLayoutParams(lp); }
这部分代码是在vp 的addOnPageChangeListener里监听获取偏移量为了保险起见:在vp 的onPageScrollStateChanged里添加监听
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) bottom_tv.getLayoutParams();
lp.leftMargin = (int) (leftTotal.get(pos) + DisplayUtil.dip2px(context, 13));
bottom_tv.setLayoutParams(lp); setSelectorUI(pos);
//这个是每个标题的宽度 private List<Integer> left = new ArrayList<>();
//这个是每个标题距离左边的总距离private List<Integer> leftTotal = new ArrayList<>();
//像素dp之间转换DisplayUtil.dip2px
//这个是刷新导航栏字体颜色的setSelectorUIpos 这个值表示当前选中的页面Utils.getTextViewLength 获取每个view 的字体宽度 public static float getTextViewLength(TextView textView, String text) {
TextPaint paint = textView.getPaint();
// 得到使用该paint写上text的时候,像素为多少
float textLength = paint.measureText(text);
return textLength; }
就是这么简单,就这么点代码就实现了导航栏滑动。。