Android广告适用的ViewPager(自定义控件)

上次看见饿了么里边的广告ViewPager感觉这个玩意还挺高大上的,所以就动手写了一个

不加Dialog的效果图:

5337239-7f4e9500e2e551bc.gif
1516065305391mzViewPAger.gif

是不是感觉,好像没啥啊?跟ViewPager有啥区别

Warning: 这个回调我没写,有项目需求的哥们,你可以自己写写

放到Dialog里边的效果图

5337239-e814cb2d39ce1d1f.gif
1516069287171mzxh.gif

现在完全展示出快这个View的漂亮了吧~~~~

本次我全部用了Kotlin语言,不得不说这个语言到现在还是不稳定,可能还是和Android Studio之间的兼容性问题

代码就不贴了,一会直接给Demo

本次代码最绕的就是这了

 override fun onTouchEvent(event: MotionEvent?): Boolean {


        when (event?.action) {
        //移动
            MotionEvent.ACTION_MOVE -> {

                //-----------------------------------操作正常滑动 ↓
                var endX: Int = event.x.toInt()

                val mid = endX - startX


                var imageX = getChildAt(viewCount).x

                // Log.e("抬起", "" + getChildAt(viewCount).x)
                try {
                    if (getChildAt(viewCount).x > 0) {
                        /**
                         * 如果小于大于0就代表往右滑了,调出隐藏在左边的View
                         */

                        getChildAt(viewCount + 1).x = getChildAt(viewCount + 1).x + mid

                        getChildAt(viewCount).alpha = -(getChildAt(viewCount + 1).x) / viewR



                        isJJ = true
                    } else {

                        /**
                         * 否则就是往左滑,隐藏当前View到右边
                         */
                        if (viewCount == childCount - 1) {
                            getChildAt(viewCount).x = imageX + mid
                        } else {

                            if (getChildAt(viewCount).x + mid > 0) {
                                /**
                                 * View坐标的调度
                                 *  写1是为了让上边的大于0产生执行,不然上边就JJ了
                                 */
                                getChildAt(viewCount).x = 1f
                            } else {
                                getChildAt(viewCount).x = imageX + mid
                            }
                        }



                        getChildAt(viewCount - 1).alpha = -(getChildAt(viewCount).x) / viewR
                        //       Log.e("透明度", "" + (-(getChildAt(viewCount).x) / viewR))
                        isJJ = false
                    }
                } catch (e: Exception) {

                }


                startX = endX
                //-----------------------------------操作正常滑动 ↑


            }
        //抬起
            MotionEvent.ACTION_UP -> {

                try {
                    if (isJJ) {

                        /**
                         * 这块用了一个ISJJ  这个变量代表着View是决定隐藏(往左滑)还是显示(往右滑)
                         *
                         *
                         * 这个区域是为了显示隐藏在右边View 简称:显示View
                         */
                        //判断是否大于左边还是小于左边
                        if (getChildAt(viewCount + 1).x > -(viewR + 20)) {
                            //大于中间滚动到第下一个


                            moveView(getChildAt(viewCount + 1).x.toInt(), 0)
                            temp = viewCount + 1
                            viewCount++

                        }

                        if (getChildAt(viewCount + 1).x < -(viewR + 20)) {
                            //小于中间滚动到上一个
                            moveView(getChildAt(viewCount + 1).x.toInt(), -viewR)

                            temp = viewCount + 1
                            if (viewCount == childCount - 1) {
                                moveView(getChildAt(viewCount).x.toInt(), 0)
                            } else {
                                viewCount++
                            }


                        }
                    } else {
                        /**
                         * 然而这个区域是为了隐藏View
                         */
                        //判断是否大于左边还是小于左边
                        if (getChildAt(viewCount).x > 0) {
                            //大于中间滚动到第下一个


                            moveView(getChildAt(viewCount).x.toInt(), 0)
                            temp = viewCount
                            viewCount++

                        }

                        if (getChildAt(viewCount).x < 0) {
                            //小于中间滚动到上一个
                            moveView(getChildAt(viewCount).x.toInt(), -viewR)


                            temp = viewCount
                            if (viewCount == 0) {
                                moveView(getChildAt(viewCount).x.toInt(), 0)
                            } else {
                                viewCount--
                            }


                        }
                    }
                } catch (e: Exception) {

                    /**
                     * 方便与程序不报错误 起见最好tryCatch一下
                     *
                     * 如果超出View有效值的范围,就初始取哪个View
                     */
                    if (viewCount >= childCount) {
                        moveView(getChildAt(childCount - 1).x.toInt(), 0)
                        viewCount = childCount - 1
                    }

                    if (viewCount <= 0) {
                        viewCount = 0
                        moveView(getChildAt(0).x.toInt(), 0)

                    }

                }


                /**
                 * 调度区域,不然每个View滑时间长了都往右走了,左边会留下一点空白,还会越留越大
                 */
                getChildAt(viewCount).x = 0f


            }
        //按下
            MotionEvent.ACTION_DOWN -> {

                /**
                 * 这个玩意是记录坐标点的
                 */
                startX = event.x.toInt()
                // Log.e("坐标记录", "startX:" + startX)

            }


        }


        return true
    }

我想着是用一个布尔值来判断,没想到还真的管用...

Demo(Github):https://github.com/hanxinhao000/XHViewPager2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值