Android如何根据手势实现顶部View自动展示与隐藏效果

发布时间:2021-07-21 14:31:26 作者:小新
来源:亿速云 阅读:127

这篇文章主要介绍Android如何根据手势实现顶部View自动展示与隐藏效果,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

首先来看一下效果:

Android如何根据手势实现顶部View自动展示与隐藏效果 

 大体思路如下:

总体布局用了一个自定义的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), MeasureSpec.makeMeasureSpec(height, 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, 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自动展示与隐藏效果”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. jQuery实现常见的隐藏与展示列表效果示例
  2. Android GestureDetector实现手势滑动效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android view

上一篇:Vue.js中有哪些实现组件通信的方式

下一篇:vue.js中todolist如何使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》