ScrollviewWithToolbar.gif
最近遇到这样的需求,一开始自己写出来,总是会有跳顿感,不是那么流畅,去网上查了下,在加上自己修改了下,便成了现在的样子。(滑动过程中上面的图片是缩放的,里面的一个小icon是缩小放大透明度渐变的,真机加Vysor,看的效果特别模糊的,没入Vysor pro,这画质将就看吧)
当然应该还可以继续优化吧,不过最近比较忙,暂时先这样吧。
为了以后方便自己整理学习,先写到简书吧。
首先初始化一些基本的参数
privatevoidinitDistance(){//这是顶部标题栏的初始高度;OrignHight = layout.getLayoutParams().height;//给它设定个上拉的最小高度;minHight = px2dp(100);//需要滑动的距离;Distance = OrignHight - minHight; }
判断ListView是否滑动到顶部;
publicbooleanisListViewReachTopEdge(ListView listView){booleanresult =false;if(listView.getFirstVisiblePosition() ==0){ View childAt = listView.getChildAt(0); result = childAt.getTop()==0; }returnresult;}
下面的是对touchevent的处理
@OverridepublicbooleandispatchTouchEvent(MotionEvent ev){//如果ListView是在顶部,进行处理,不在顶部,不执行 if(isListViewReachTopEdge(listView)){inty = (int) ev.getY();intdistanceY =0;switch(ev.getAction()) {caseMotionEvent.ACTION_DOWN: pointY = y; afterup =false;returnsuper.dispatchTouchEvent(ev);caseMotionEvent.ACTION_MOVE: y = (int) ev.getY();if(currentDistance >= Distance && distanceY >0) {returnsuper.dispatchTouchEvent(ev); }if(currentDistance <=0&& distanceY <0) {returnsuper.dispatchTouchEvent(ev); } distanceY = pointY - y;if(rate ==1) {if(distanceY >0) {returnsuper.dispatchTouchEvent(ev); } } moveLayout(distanceY); pointY = y;break;caseMotionEvent.ACTION_UP: afterup =true; elasticityHeight();returnsuper.dispatchTouchEvent(ev); } }else{returnsuper.dispatchTouchEvent(ev); }returnfalse;}```接下来就是里面用的各种方法```java//滑动过程中的处理privatevoidmoveLayout(intdistanceY){ ViewGroup.LayoutParams layoutParams = layout.getLayoutParams(); layoutParams.height = layoutParams.height - distanceY; layout.setLayoutParams(layoutParams); elasticityHeight(); layout.requestLayout(); currentDistance = OrignHight - layout.getLayoutParams().height; rate = (float) (currentDistance *1.0/ Distance); moveViewByrate(rate);}
//抬手后进行的处理privatevoidelasticityHeight(){ ViewGroup.LayoutParams layoutParams = layout.getLayoutParams();if(layoutParams.height < minHight) { layoutParams.height = minHight; layout.setLayoutParams(layoutParams); layout.requestLayout(); }if(layoutParams.height > OrignHight) { layoutParams.height = OrignHight; layout.setLayoutParams(layoutParams); layout.requestLayout(); }if(afterup) {if(layoutParams.height < OrignHight /1.8) { layoutParams.height = minHight; layout.setLayoutParams(layoutParams); imageView.setVisibility(View.GONE); layout.requestLayout(); }if(layoutParams.height >= OrignHight /1.8) { layoutParams.height = OrignHight; layout.setLayoutParams(layoutParams); imageView.setVisibility(View.VISIBLE); imageView.setAlpha((float)1.0-0); imageView.setScaleX(1); imageView.setScaleY(1); layout.requestLayout(); } }}
//滑动过程中,控件的大小,透明度的变化privatevoidmoveViewByrate(floatrate){if(rate >=1) { imageView.setVisibility(View.GONE); }else{ imageView.setVisibility(View.VISIBLE); imageView.setAlpha(1- rate); imageView.setScaleX(1- rate); imageView.setScaleY(1- rate); }}
文章中有部分是转载自网络,在这里说声抱歉。
因为时间仓促,里面可能有些错误,希望指正。
作者:一脸懵逼的大林子
链接:https://www.jianshu.com/p/56b78a948ded
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。