自己动手实现一个可垂直滑动的ViewGroup,相当于可以垂直滑动的viewpager

原创 2015年07月09日 15:47:23

前言

      1,理解ScrollTo(x,y)和ScrollBy(x,y)的区别,如果不知请自行百度

     2,重写ViewGroup,相关知识自行百度

     3,   资源下载http://download.csdn.net/detail/u012155141/8894845

    4,效果图

      

     

进入正题

    1,重写ViewGroup

三个构造方法和一些需要初始化的内容

      public CustomScrollLayout(Context context) {
this(context, null);
}


public CustomScrollLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}


public CustomScrollLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);


mScroller = new Scroller(context);


mGestureDetector = new GestureDetector(new IGestureDetecror());


ViewConfiguration configuration = ViewConfiguration.get(context);
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();


}


2,重写OnMeasure和OnLayout,在简单化的考虑下本例只在确定尺寸下运行

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);


int height = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);


if (heightMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException(
"ScrollLayout only can run at EXACTLY mode!");
}


int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
view.measure(widthMeasureSpec, heightMeasureSpec);
}
scrollTo(0, 0);
}


protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
int childCount = getChildCount();
int scrollHeight = 0;
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
int height = view.getMeasuredHeight();


view.layout(0, scrollHeight, view.getMeasuredWidth(), scrollHeight
+ height);
scrollHeight += height;
}
}

3,在手指按下到移动这个过程中事件在mGestureDetector.onTouchEvent(event)中处理,用ScrollBy(x,y)来让view中的内容跟随手指移动;

class IGestureDetecror implements OnGestureListener {


@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}


@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}


@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub


}


@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// TODO Auto-generated method stub
Log.i("getScrollY=", "" + getScrollY());
scrollBy(0, (int) distanceY);
Log.i("AftergetScrollY=", "" + getScrollY());
return false;
}


@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub


}


@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}


}

4,在onTouchEvent(MotionEvent event)中从手指按下到移动的过程我们在mGestureDetector.onTouchEvent(event)中处理,当手指离开时我们在Up中处理,用ScrollTo(x,y)进行处理

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub


mGestureDetector.onTouchEvent(event);


int action = event.getAction();
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();// 获得VelocityTracker类实例
}
mVelocityTracker.addMovement(event);// 将事件加入到VelocityTracker类实例中
mVelocityTracker.computeCurrentVelocity(1000);


switch (action) {
case MotionEvent.ACTION_DOWN:
firstY = (int) event.getY();
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:


float yVelocity = Math.abs(mVelocityTracker.getYVelocity());
if (yVelocity > mMaximumVelocity) {
yVelocity = mMaximumVelocity;
}


int tmpId = 0;
int currentY = (int) event.getY();
if (currentY - firstY > getHeight() / 2) {
tmpId = currentId - 1;
} else if (firstY - currentY > getHeight() / 2) {
tmpId = currentId + 1;
} else if ((yVelocity > mMinimumVelocity)
&& yVelocity <= mMaximumVelocity) {
if (currentY - firstY > 0) {
tmpId = currentId - 1;
} else {
tmpId = currentId + 1;
}
} else {
tmpId = currentId;
}


int childCount = getChildCount();
currentId = tmpId < 0 ? 0
: ((tmpId > childCount - 1) ? childCount - 1 : tmpId);
mScroller.startScroll(0, getScrollY(), 0, currentId * getHeight()
- getScrollY());
invalidate();
// scrollTo(0, currentId*getHeight());


break;
case MotionEvent.ACTION_CANCEL:
mVelocityTracker.recycle();
break;


default:
break;
}


return true;


}


@Override
public void computeScroll() {
// TODO Auto-generated method stub
super.computeScroll();
if (mScroller.computeScrollOffset()) {
Log.i("======", "=========");
Log.i("getScrollY=", "" + getScrollY());
Log.i("mScroller.getFinalY()=", "" + mScroller.getFinalY());
Log.i("mScroller.getCurrY()=", "" + mScroller.getCurrY());
scrollTo(0, mScroller.getCurrY());
invalidate();
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Android 垂直滑动ViewPager实现

今天在用QQ电影的时候,里面热门电影展示是一个类似ViewPager垂直滑动的效果,感觉很新奇,于是乎在网上找了找,最后找到JakeWharton的一个开源项目:Android-Directional...
  • FX_SKY
  • FX_SKY
  • 2013-12-24 15:13
  • 9277

ViewGroup垂直滑动

  • 2015-09-06 15:45
  • 1.68MB
  • 下载

自定义RecyclerView实现垂直滑动的ViewPager

android原生的ViewPager默认水平方向滑动翻页的。突然,项目里有个签到需求要做成上下滑动翻页。我的第一反应是应用ViewPager,可是它只适用于水平滑动的情景,可不可以继承ViewPag...

垂直方向滑动的ViewPager

  • 2013-07-17 19:05
  • 1.09MB
  • 下载

自定义垂直方向滑动的ViewGroup

自定义竖直方向滚动的ViewGroup,类似于竖直方向的VeiwPager

垂直滑动的ViewPager

  • 2014-05-19 15:24
  • 2.02MB
  • 下载

垂直滑动的Viewpager

  • 2017-09-09 12:33
  • 18.88MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)