BaseTabStrip-用于捆绑ViewPager实现复杂页面

2016-08-12  本文已影响0人  AlexMofer

BaseTabStrip

ICON

支持库v4中本就存在控件PagerTitleStrip,但对于各式各样的需要,其还是满足不了。但每一种样式都重新写一些基础方法也浪费,于是将通用方法提出写成基类,而具体样式就交给子类实现。
继承自View,可自动捆绑ViewPager,BaseTabStrip为基础类,仅实现了一些通用基础逻辑。具体的实现效果需要实现与重写部分方法。

要求

链接

使用

必须实现的方法(实现子类独有的效果):

    /**
     * 直接跳转到
     *
     * @param current 位置
     */
    protected abstract void jumpTo(int current);

    /**
     * 滑向左边
     *
     * @param current 当前页
     * @param next    目标页
     * @param offset  偏移
     */
    protected abstract void gotoLeft(int current, int next, float offset);

    /**
     * 滑向右边
     *
     * @param current 当前页
     * @param next    目标页
     * @param offset  偏移
     */
    protected abstract void gotoRight(int current, int next, float offset);

可重写的方法:
完成PagerAdapter绑定,但并未刷新界面及布局:

    /**
     * 捆绑PagerAdapter
     */
    protected void onBindPagerAdapter() {

    }

若子类要实现点击事件,需要实现将点击的点转化为子项Position:

    /**
     * 由触摸点转为Position
     *
     * @param x X坐标
     * @param y Y坐标
     * @return 坐标对应位置
     */
    protected int pointToPosition(float x, float y) {
        return 0;
    }

若设置了子项的Background,需要设置Background的hotspot,则需要重写:

    /**
     * set hotspot's x location
     *
     * @param background 背景图
     * @param position   图片Position
     * @param motionX    点击位置X
     * @param motionY    点击位置Y
     * @return x location
     */
    protected float getHotspotX(Drawable background, int position, float motionX, float motionY) {
        return background.getIntrinsicWidth() * 0.5f;
    }

    /**
     * set hotspot's y location
     *
     * @param background 背景图
     * @param position   图片Position
     * @param motionX    点击位置X
     * @param motionY    点击位置Y
     * @return y location
     */
    protected float getHotspotY(Drawable background, int position, float motionX, float motionY) {
        return background.getIntrinsicHeight() * 0.5f;
    }

子项类实现@ViewPager.DecorView接口用于达到作为ViewPager子项显示

注意

上一篇 下一篇

猜你喜欢

热点阅读