ConvenientBanner在TabHost下tab切换出现空白解决方案

在tabHost下,由于重写了onCreateView,保存fragment实例,导致了Tab切换时,ConvenientBanner在最后一张图准备轮播到第一张图,会出现空白。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(layoutInit(), container, false);
        }
        // 缓存的rootView需要判断是否已经被加过parent,如果有parent需要从parent删除,要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }

找了很久没有找到原因,后面找到一个偏门的解决方法,在切换TAB之前将currentItem设为0。

解决代码:

public class ConvenientBanner<T> extends LinearLayout {

*****************************省略其他代码************************

/***
     * 开始翻页(第一次翻页)
     * 
     * @param autoTurningTime
     *            自动翻页时间
     * @return
     */
    public ConvenientBanner startTurning(long autoTurningTime) {
        // 如果是正在翻页的话先停掉
        if (turning) {
            stopTurning();
        }
        // 初始化page(否则在tabhost中会出现白屏)
        viewPager.setCurrentItem(0);
        // 设置可以翻页并开启翻页
        canTurn = true;
        this.autoTurningTime = autoTurningTime;
        turning = true;
        timeHandler.postDelayed(adSwitchTask, autoTurningTime);
        return this;
    }

    /***
     * 重新开始翻页(手指触碰暂停翻页,松开之后的重新翻页,不需要setItem为0)
     * 
     * @param autoTurningTime
     *            自动翻页时间
     * @return
     */
    public ConvenientBanner reStartTurning(long autoTurningTime) {
        // 如果是正在翻页的话先停掉
        if (turning) {
            stopTurning();
        }
        // 设置可以翻页并开启翻页
        canTurn = true;
        this.autoTurningTime = autoTurningTime;
        turning = true;
        timeHandler.postDelayed(adSwitchTask, autoTurningTime);
        return this;
    }
// 触碰控件的时候,翻页应该停止,离开的时候如果之前是开启了翻页的话则重新启动翻页
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        if (ev.getAction() == MotionEvent.ACTION_UP) {
            // 重新开始翻页
            if (canTurn)
                reStartTurning(autoTurningTime);
        } else if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            // 停止翻页
            if (canTurn)
                stopTurning();
        }
        return super.dispatchTouchEvent(ev);
    }
}

如果有朋友能找到根本原因,希望能给我留言,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值