
首先来看一下效果:
大体思路如下:
总体布局用了一个自定义的VIEwGroup,里面包了两个VIEw(top VIEw,bottomVIEw)
我在bottomVIEw里放了VIEwPager,里面又有Fragment,Fragment里放的是ListVIEw
原理:
VIEwGroup在分发touchEvent的时候先通过手势GestureDetector判断手势方向,当向上滑动的时候让topVIEw和bottomVIEw同时向上移动,反之亦然。
整体思路不是很难如下是干货:
布局文件
<com.lin.gesturedetector.MyVIEwGroup androID:ID="@+ID/vIEw_group" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <include androID:ID="@+ID/group_top" layout="@layout/vIEw_top" /> <include androID:ID="@+ID/group_bottom" layout="@layout/vIEw_bottom" /> </com.lin.gesturedetector.MyVIEwGroup>
手势监听重要的是打log看一下上下滑动是数值的变化,找到其规律:
@OverrIDe public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY) { Log.i(tag,"onScroll -> distanceY" + distanceY); if (distanceY < 0) {// 手势向下滑动是负值 animatorLayoutOffset(1); } if (distanceY > 0) { animatorLayoutOffset(0f); } return true; }一定记得在VIEwGroup内查找控件需要在onFinishInflate后才能找到:
@OverrIDe protected voID onFinishInflate() { super.onFinishInflate(); vIEwtop = findVIEwByID(R.ID.group_top); vIEwBottom = findVIEwByID(R.ID.group_bottom); }在VIEwGroup布局的逻辑中需要处理的有一下几点:
1、onMeasure的时候要把子控件测量出来
2、onLayout时需要手动将子控件布局
接下来就是监听手势设置动画,不停的onLayout以达到topVIEw和bottomVIEw的布局效果
@OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { int wIDth = MeasureSpec.getSize(wIDthMeasureSpec); int height = MeasureSpec.getSize(heightmeasureSpec); vIEwtop.measure(MeasureSpec.makeMeasureSpec(wIDth,MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(height,MeasureSpec.AT_MOST)); vIEwBottom.measure(MeasureSpec.makeMeasureSpec(wIDth,MeasureSpec.EXACTLY)); setMeasuredDimension(wIDth,height); } @OverrIDe protected voID onLayout(boolean changed,int l,int t,int r,int b) { int topHeight = vIEwtop.getMeasuredHeight(); float offset = layoutOffset * topHeight; int wIDth = r - l; float topVIEwYtop = offset - topHeight; float topVIEwYBottom = topVIEwYtop + topHeight; vIEwtop.layout(0,(int) topVIEwYtop,wIDth,(int) topVIEwYBottom); vIEwBottom.layout(0,(int) topVIEwYBottom,(int) topVIEwYBottom + vIEwBottom.getMeasuredHeight()); } private voID animatorLayoutOffset(float offset) { if (animator != null && animator.isRunning()) { return; } animator = ObjectAnimator.offloat(this,"layoutOffset",layoutOffset,offset); animator.setDuration(500); animator.start(); }项目地址在这:
GitHub
总结
以上所述是小编给大家介绍的AndroID 根据手势顶部VIEw自动展示与隐藏效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android 根据手势顶部View自动展示与隐藏效果全部内容,希望文章能够帮你解决Android 根据手势顶部View自动展示与隐藏效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)