茂名网站建设培训,linux网页制作软件,广州南沙区建设局网站,网站建设制作费用预算表首先来看一下效果#xff1a;大体思路如下#xff1a;总体布局用了一个自定义的ViewGroup#xff0c;里面包了两个View(top View#xff0c;bottomView)我在bottomView里放了ViewPager#xff0c;里面又有Fragment#xff0c;Fragment里放的是ListView原理#xff1a;Vi…首先来看一下效果大体思路如下总体布局用了一个自定义的ViewGroup里面包了两个View(top ViewbottomView)我在bottomView里放了ViewPager里面又有FragmentFragment里放的是ListView原理ViewGroup在分发touchEvent的时候先通过手势GestureDetector判断手势方向当向上滑动的时候让topView和bottomView同时向上移动反之亦然。整体思路不是很难如下是干货布局文件android:idid/view_groupandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:idid/group_toplayoutlayout/view_top /android:idid/group_bottomlayoutlayout/view_bottom /手势监听重要的是打log看一下上下滑动是数值的变化找到其规律Overridepublic 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后才能找到Overrideprotected void onFinishInflate() {super.onFinishInflate();viewTop findViewById(R.id.group_top);viewBottom findViewById(R.id.group_bottom);}在ViewGroup布局的逻辑中需要处理的有一下几点1、onMeasure的时候要把子控件测量出来2、onLayout时需要手动将子控件布局接下来就是监听手势设置动画不停的onLayout以达到topView和bottomView的布局效果Overrideprotected 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), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));setMeasuredDimension(width, height);}Overrideprotected 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, width, (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();}项目地址在这总结以上所述是小编给大家介绍的Android 根据手势顶部View自动展示与隐藏效果希望对大家有所帮助如果大家有任何疑问请给我留言小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持