版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Maiduoudo/article/details/84768726
@Override
public void onPageSelected(int position) {
setCurrentTab(position);
}
实现滑动标题时始终居中的函数
public void setCurrentTab(int position){
for(int i=0;i<arrTextView.length;i++){
arrTextView[i].setTextColor(Color.BLACK);
}
arrTextView[position].setTextColor(Color.RED);
//获取屏幕宽度
int screenWidth=getResources().getDisplayMetrics().widthPixels;
//计算控件居正中时距离左侧屏幕的距离
int middleLeftPosition=(screenWidth-arrTextView[position].getWidth())/2;
//正中间位置需要向左偏移的距离
int offset=arrTextView[position].getLeft()-middleLeftPosition;
//让水平的滚动视图按照执行的x的偏移量进行移动
hs.smoothScrollTo(offset,0);
}