viewpager 和滑动条同步滚动

实现上面的radiobutton和viewpager同步滚动太过复杂,请直接使用开源项目PagerSlidingTabStrip

共有6个的页面,导航栏用horizontalscrollview封装radiogroup。点击radiobutton跳转到对应的页面。滑动pager也可以让radiogroup一起滑动。

自定义进度条view,关联viewpager的onPageScolled()方法。onPageScolled中在调用setOffSet()。即可实现滑动条和页面同步滚动。



public class StripView extends View {


    private int mTabNum=6;//标题数目,
    private int mCurrentNum;//当前所在标题
    private float mWidth,mTabWidth,mOffSet;
    private final Paint mPaint= new Paint(Paint.ANTI_ALIAS_FLAG);
    public StripView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public void setCurrentNum(int n) {
        mCurrentNum = n;
        mOffSet = 0;
    }
    public void setOffSet(int position,float offset){
        if(offset == 0) {
            return;
        }
        mCurrentNum = position;
        mOffSet=offset;
        invalidate();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
            mWidth = getWidth();
            mTabWidth = mWidth/6;
        float left = (mCurrentNum + mOffSet) * mTabWidth;


        final float top = getPaddingTop();
        final float right = left + mTabWidth;
        final float bottom = getHeight() +getPaddingTop();
        mPaint.setColor(getResources().getColor(R.color.cyan));
        canvas.drawRect(left, top, right, bottom, mPaint);


    }
}



你可能感兴趣的:(viewpager 和滑动条同步滚动)