当前位置:编程学习 > wap >>

滑动导航条问题

想实现一个滑动的导航条,在页面切换时导航条上的文字颜色发生变化,同时下面会有红色的线跟着滑动,我的想法是在每次滑动之后就获取当前导航条上的文字的位置,然后红色的线从上次位置滑动到当前获得的位置,但是在滑动页面时红色的线条的位置总是和当前的选项不对应,但是点击每个选项时位置却是对的,
这个是滑动页面时的情况

这个是点击时的情况

代码有什么错误吗?该怎么解决求解答

public void setSelector(int id) {
for (int i = 0; i < title.length; i++) {
if (id == i) {
textViews.get(id).setTextColor(Color.RED);
if (i > 2) {
horizontalScrollView.smoothScrollTo(textViews.get(id).getLeft()-200, 0);
} else {
horizontalScrollView.smoothScrollTo(0, 0);
}
viewPager.setCurrentItem(i);
} else {
textViews.get(i).setTextColor(Color.BLACK);
}
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout = (LinearLayout) findViewById(R.id.ll_main);
viewPager = (ViewPager) findViewById(R.id.pager);
horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView);
imageView= (ImageView) findViewById(R.id.cursor);
InItTitle1();
setSelector(0);
InItView();
viewPager.setAdapter(new myPagerView());
viewPager.clearAnimation();
viewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int arg0) {
setSelector(arg0);
textViews.get(arg0).getLocationInWindow(location);
LinearLayout.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.width = textViews.get(arg0).getWidth();
layoutParams.height = 4;
imageView.setLayoutParams(layoutParams);
System.out.println("x "+arg0+"="+location[0]);
animation = new TranslateAnimation(currIndex,location[0], 0, 0);
currIndex = location[0];
animation.setFillAfter(true);// True:图片停在动画结束位置
animation.setDuration(500);
imageView.startAnimation(animation);
//
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override
public void onPageScrollStateChanged(int arg0) {

}
});
}

@Override
public void onClick(View v) {
// viewPager.setCurrentItem(v.getId());
setSelector(v.getId());
// v.getLocationInWindow(location);
System.out.println(location[0]);

}
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,