自定义滑动TAB

public class CustomScrollBarWidget extends FrameLayout implements ChangePage{
private Scroller mScroller;
private int mPageCount;
private int mWidth;
private ChangePage changePage;
private String TAG = "CustomScrollBarWidget";


public CustomScrollBarWidget(Context context, int pageCount, int width) {
super(context);
mScroller = new Scroller(context);
mPageCount = pageCount;
ImageView imageView = new ImageView(context);
mWidth = width / mPageCount;
LayoutParams params = new LayoutParams(mWidth, LayoutParams.WRAP_CONTENT);
imageView.setBackgroundResource(R.drawable.set_title_select);
addView(imageView, params);
}


@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}


private void setToScreen(int whichScreen) {
if (!mScroller.isFinished()) {
mScroller.abortAnimation(); // 终止Animation
}
scrollTo(whichScreen * mWidth, 0);
}


private void snapToScreen(int whichScreen) {
whichScreen = Math.max(0, Math.min(whichScreen, mPageCount - 1));
if(getScrollX() != (whichScreen * mWidth)){
final int delta = -whichScreen * mWidth - getScrollX();
mScroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta) * 2);
invalidate(); // 重新布局
}
}


public void setChagePage(ChangePage changePage){
this.changePage = changePage;
}

@Override
public void changePage(int pageNumber) {
snapToScreen(pageNumber);
if (changePage != null) {
changePage.changePage(pageNumber);
}

}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值