温馨提示×

温馨提示×

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

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

通过在Android中自定义StickinessView实现一个粘性滑动效果

发布时间:2020-11-25 17:03:34 来源:亿速云 阅读:152 作者:Leah 栏目:移动开发

这篇文章给大家介绍通过在Android中自定义StickinessView实现一个粘性滑动效果,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

一、首先,要确定HeadLayout什么时候可以拦截事件,那么就要确定ListView到达顶部和底部的时机。

 @Override
 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
  View v = mListView.getChildAt(0);
  //当firstItem的top为0的时候就认为已经到达ListView的顶部了
  if (mListView.getChildCount() > 0 && firstVisibleItem == 0) {
   //滑动到顶部
   if (v.getTop() == 0) {
    //滑动到顶部了
    isListViewTop = true;
   } else {
    isListViewBottom = false;
   }
  }else if (mListView.getChildCount()>0&&firstVisibleItem+visibleItemCount==totalItemCount){
   final View bottomChildView = mListView.getChildAt(mListView.getChildCount()-1);
//当最后一个itemView的bottom>=ListView的高度的时候,那么就认为到达底部了
   if    (mListView.getHeight()>=bottomChildView.getBottom()){
    isListViewBottom = true;
   }else {
    isListViewBottom = false;
   }
  }else {
   isListViewBottom = false;
   isListViewTop = false;
  }

原因很简单,因为View的getTop和getBottom方法是相对父容器的位置,熟悉Layout方法的,想必就会很明白了。

二、知道了HeadView拦截事件的时机,我们就要搞清楚在此基础之上,我们到底啥时候拦击点击事件,进行滑动。

 @Override
 public boolean onInterceptTouchEvent(MotionEvent ev) {
  switch (ev.getAction()) {
   case MotionEvent.ACTION_DOWN:
    touchY = ev.getRawY();
    isIntercept = false;
    break;
   case MotionEvent.ACTION_MOVE:
    float distant = ev.getRawY() - touchY;
    if (isListViewTop) {
     switch (mHeadPosition) {
      case TOP:
       if (distant > 0) isIntercept = true;
       break;
      case CENTER:
       isIntercept = true;
       break;
     }
    }
    if (isListViewBottom){
     switch (mHeadPosition) {
      case CENTER:
       isIntercept = true;
       break;
      case BOTTOM:
       if (distant < 0) isIntercept = true;
       break;
     }
    }

    break;
   case MotionEvent.ACTION_UP:
    isIntercept = true;
    break;
  }
  return isIntercept;
 }

跟大家讲解一下onInterceptTouchEvent(MotionEvent ev),这个方法会最先调用,当一个事件序列拦截一次后,那么这个事件的后续事件动作就不会再调用该方法,也就是说,当该ViewGroup决定拦截某个事件后,那么它注定要消费后续的事件动作。这里贴出HeadView的位置状态

public static final int TOP = 0;//收缩状态
public static final int CENTER = 1;//中间状态
public static final int BOTTOM = 2;//展开状态

关于细节,想必大家画个图就可以知道了,注意一点:在拦截事件序列的时候,一般ACTION_DOWN事件不可以被拦截,因为拦截的话,没得意义了,后续事件就无法控制了,不可能继续往ChildView传递事件序列。

三、移动HeadView。

@Override
public boolean onTouchEvent(MotionEvent event) {
 switch (event.getAction()) {
  case MotionEvent.ACTION_DOWN:
   //获取不到的
   break;
  case MotionEvent.ACTION_MOVE:
   int distant = (int) (touchY - event.getRawY());
   if (getScrollY() + distant-1 < MAXY && getScrollY() + distant > 0) {
    scrollTo(0, getScrollY() + distant);
   }
   break;
  case MotionEvent.ACTION_UP:
   if (getScrollY() == 0) mHeadPosition = BOTTOM;
   if (getScrollY() == MAXY) mHeadPosition = TOP;
   if (getScrollY() > 0 && getScrollY() < MAXY) mHeadPosition = CENTER;
   if (getScrollY() > MAXY / 2) {
    mScroll.startScroll(0, getScrollY(), 0, MAXY-getScrollY(),100);
    invalidate();
    mHeadPosition = TOP;
   }
   if (getScrollY() < MAXY / 2) {
    mScroll.startScroll(0, getScrollY(),0,-getScrollY(),100);
    invalidate();
    mHeadPosition = BOTTOM;
   }
   break;
 }
 return super.onTouchEvent(event);
}

这里为了使得滑动跟家顺畅我使用了Scroller这个类,该类是专门处理弹性滑动的工具类,先初始化构造器,在调用startScroll()方法(其中四个参数:滑动的x,滑动的y,滑动x的偏移量,滑动y的偏移量),然后刷新视图,最后重写computeScroll()方法,

@Override
public void computeScroll() {
 super.computeScroll();
 if (mScroll.computeScrollOffset()){
  scrollTo(mScroll.getCurrX(),mScroll.getCurrY());
  postInvalidate();
 }
}

好了,基本完成,我们还要第一时间获取HeadView的高度,那么在onMeasure()中获取比较好,并且只获取一次如下

 if (MAXY == -1)
  MAXY = mHeadSecond.getMeasuredHeight();

在onFinishInflate()方法中,该方法的执行标志着所有的View都已经add完毕,这里我们进行初始化是比较妥当的。

 @Override
  protected void onFinishInflate() {
  super.onFinishInflate();
  int count = getChildCount();
  //本粘性布局只支持ListView
  if (count == 3 && getChildAt(2) instanceof ListView)
   init();
 }
 /**
  * 初始化
  */
 private void init() {
  //获得子元素
  mHeadFiest = getChildAt(0);
  mHeadSecond = getChildAt(1);
  mListView = (ListView) getChildAt(2);
  mListView.setOnScrollListener(this);
  mScroll = new Scroller(getContext());
 }

关于通过在Android中自定义StickinessView实现一个粘性滑动效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

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

AI