美文网首页Android知识
跟随ListView滑动而变化的标题栏

跟随ListView滑动而变化的标题栏

作者: 一脸懵逼的大林子 | 来源:发表于2016-12-14 13:18 被阅读0次
ScrollviewWithToolbar.gif

最近遇到这样的需求,一开始自己写出来,总是会有跳顿感,不是那么流畅,去网上查了下,在加上自己修改了下,便成了现在的样子。(滑动过程中上面的图片是缩放的,里面的一个小icon是缩小放大透明度渐变的,真机加Vysor,看的效果特别模糊的,没入Vysor pro,这画质将就看吧)

当然应该还可以继续优化吧,不过最近比较忙,暂时先这样吧。

为了以后方便自己整理学习,先写到简书吧。

首先初始化一些基本的参数

private void initDistance() {    
      //这是顶部标题栏的初始高度;
     OrignHight = layout.getLayoutParams().height;    
     //给它设定个上拉的最小高度;
     minHight = px2dp(100);    
     //需要滑动的距离;
     Distance = OrignHight - minHight; 
}

判断ListView是否滑动到顶部;

public boolean isListViewReachTopEdge(ListView listView){    
  boolean result = false;    
  if(listView.getFirstVisiblePosition() == 0){        
    View childAt = listView.getChildAt(0);        
    result = childAt.getTop()==0;    
  }    
  return result;
}

下面的是对touchevent的处理

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
//如果ListView是在顶部,进行处理,不在顶部,不执行    
  if(isListViewReachTopEdge(listView)){       
     int y = (int) ev.getY();       
     int distanceY = 0;        
     switch (ev.getAction()) {            
       case MotionEvent.ACTION_DOWN:           
         pointY = y;                
         afterup = false;                
         return super.dispatchTouchEvent(ev);            
       case MotionEvent.ACTION_MOVE:       
         y = (int) ev.getY();             
        if (currentDistance >= Distance && distanceY > 0) {                    
          return super.dispatchTouchEvent(ev);                
        }                
        if (currentDistance <= 0 && distanceY < 0) {                    
          return super.dispatchTouchEvent(ev);                
        }                
        distanceY = pointY - y;                
        if (rate == 1) {                    
          if (distanceY > 0) {                        
            return super.dispatchTouchEvent(ev);                    
          }                
        }                
        moveLayout(distanceY);                
        pointY = y;                
        break;            
       case MotionEvent.ACTION_UP:                
        afterup = true;                
        elasticityHeight();                
        return super.dispatchTouchEvent(ev);        
      }    
  }else {        
    return super.dispatchTouchEvent(ev);    
    }    
   return false;
}```

接下来就是里面用的各种方法

```java
//滑动过程中的处理
private void moveLayout(int distanceY) {
    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);}
//抬手后进行的处理
private void elasticityHeight() {
    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();
        }
    }
}
//滑动过程中,控件的大小,透明度的变化
private void moveViewByrate(float rate) {
        if (rate >= 1) {
            imageView.setVisibility(View.GONE);
        } else {
            imageView.setVisibility(View.VISIBLE);
            imageView.setAlpha(1 - rate);
            imageView.setScaleX(1 - rate);
            imageView.setScaleY(1 - rate);
        }
}

文章中有部分是转载自网络,在这里说声抱歉。
因为时间仓促,里面可能有些错误,希望指正。

相关文章

网友评论

    本文标题:跟随ListView滑动而变化的标题栏

    本文链接:https://www.haomeiwen.com/subject/mzjimttx.html