简单的仿淘宝京东详情页效果

·  阅读 177

自定义ScrollView实现ScrollView不惯性滑动

class NoFlyingScrollView(context:Context, attr: AttributeSet):ScrollView(context,attr){
    override fun fling(velocityY: Int) {
//        super.fling(0)
    }
}
复制代码

Touch事件监听

scroll.setOnTouchListener({v,event->
            when(event.action){
                MotionEvent.ACTION_CANCEL,
                MotionEvent.ACTION_UP -> {
                    Log.d("--->","scroll.height="+scroll.height+"   event.rawY="+event.rawY+"  top.height="+top.height+"  scroll.scrollY"+scroll.scrollY+"  top.scrollY="+top.scrollY)
                    if(showingTop){//正在展示顶部
                        if(scroll.height+scroll.scrollY<top.height){//小于topView的高度
                            showingTop=true
                            return@setOnTouchListener false
                            //滚动高度大于topView小于topView+安全距离
                        }else if(scroll.height+scroll.scrollY>top.height&&scroll.height+scroll.scrollY<top.height+spaceDistance){
                            scroll.smoothScrollTo(0,top.height-scroll.height)
                            showingTop = true
                            return@setOnTouchListener true
                            //滚动高度大于topview高度加安全距离
                        }else if(scroll.height+scroll.scrollY>top.height+spaceDistance){
                            scroll.smoothScrollTo(0,top.height)
                            showingTop = false
                            return@setOnTouchListener true
                        }
                    }else{//正在展示下面的view
                        //滚动高度小于topview-安全距离
                        if (scroll.scrollY<top.height-spaceDistance){
                            scroll.smoothScrollTo(0,0)
                            showingTop = true
                            return@setOnTouchListener true
                            //滚动高度大于topView-安全距离且小于topView
                        }else if(scroll.scrollY>top.height-spaceDistance&&scroll.scrollY<top.height){
                            scroll.smoothScrollTo(0,top.height)
                            showingTop = false
                            return@setOnTouchListener true
                        }else if(scroll.scrollY>top.height){
                            showingTop = false
                            return@setOnTouchListener false
                        }
                    }

                }
                MotionEvent.ACTION_MOVE->{
                }
            }
            false
        })
复制代码
收藏成功!
已添加到「」, 点击更改