tabhost+滚动条的效果实现

方案1:
tabhost+viewPager
参考连接:
http://blog.csdn.net/qjlhlh/article/details/7788444
github上有更强大,以封装好的相关类库
连接为:https://github.com/LuckyJayce/ViewPagerIndicator
方案2:
在tabhost所在xml写一个View占位符,摆在tabhost之上;
根据选项卡的个数代码计算占位符宽度;
关键代码:
代码计算滚动条宽度:
/**
     * 初始滚动条长度
     */
    private void InitImageView() {
        topSelect = (ImageView) findViewById(R.id.tab_top_select);
        //有几张图片就让其便宜屏幕的多少分之一
        int screenW = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
        int offset = screenW / 4;// 计算偏移量
        Matrix matrix = new Matrix();
        matrix.postTranslate(0, 0);
        topSelect.setImageMatrix(matrix);// 设置动画初始位置
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) topSelect.getLayoutParams();
        params.width = offset;
        topSelect.setLayoutParams(params);
    }


在tabhost切换时执行
/**
     * 移动tab选中标识图片
     *
     * @param selectIndex
     */
    public void moveTopSelect(int selectIndex) {
// 起始位置中心点
        TabWidget tabwidget = mFragmentTabHost.getTabWidget();
        int startMid = ((View) tabwidget.getChildAt(mCurSelectTabIndex)).getLeft() +
                ((View) tabwidget.getChildAt(mCurSelectTabIndex)).getWidth() / 2;
// 起始位置左边位置坐标
        int startLeft = startMid - topSelect.getWidth() / 2;
// 目标位置中心点
        int endMid = ((View) tabwidget.getChildAt(selectIndex)).getLeft() +
                ((View) tabwidget.getChildAt(selectIndex)).getWidth() / 2;
// 目标位置左边位置坐标
        int endLeft = endMid - topSelect.getWidth() / 2;
        TranslateAnimation animation = new TranslateAnimation(startLeft, endLeft - topSelect.getLeft(), 0, 0);
        animation.setDuration(200);
        animation.setFillAfter(true);
        topSelect.bringToFront();
        topSelect.startAnimation(animation);
// 改变当前选中按钮索引
        mCurSelectTabIndex = selectIndex;
        Log.i("fs", "endMid " + endMid + " startLeft " + startLeft + " endLeft" + (endLeft - topSelect.getLeft()));
    }


参考连接:
http://blog.csdn.net/jdsjlzx/article/details/7555408


比较:使用方案1,手势操作也支持,不过给的demo是滚动条向下的;
方案2,手势操作需要另加,滚动条调位置比较方便
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值