经常看到ios应用有这样的效果,即使控件已经滑动到顶部了,仍然可以往下继续滑。而我们用scrollview写出相同的界面,滑动到顶部或者底部的时候就很僵了。反正,继续滑是不可能继续滑的,用ios控件来写android又不会,只有自己改个控件才能维持得了生活这个样子。
效果大概就是这样。
代码比较简单,就直接贴了 public class MyScrollView extends ScrollView { private static final int ratio = 4; private View inner; private Rect normal = new Rect();; private float oldY; public MyScrollView(Context context) { super(context); } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onFinishInflate() { super.onFinishInflate(); if (getChildCount() > 0) { inner = getChildAt(0); } } @Override public boolean onTouchEvent(MotionEvent ev) { if (inner == null) { return super.onTouchEvent(ev); } else { processTouchEvent(ev); } return super.onTouchEvent(ev); } public void processTouchEvent(MotionEvent ev) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: oldY = ev.getY(); break; case MotionEvent.ACTION_MOVE: final float preY = oldY; float nowY = ev.getY(); /** * ratio=4 表示view移动的距离为拖动距离的1/4 */ int deltaY = (int) (preY - nowY) / ratio; oldY = nowY; //是否需要移动布局 if (isNeedMove()) { if (normal.isEmpty()) { // 保存正常的布局位置 normal.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom()); return; } int yy = inner.getTop() - deltaY; // 移动布局 inner.layout(inner.getLeft(), yy, inner.getRight(), inner.getBottom() - deltaY); } break; case MotionEvent.ACTION_UP: if (isNeedAnimation()) { animation(); } break; default: break; } } // 开启动画移动 public void animation() { // 开启移动动画 TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(), normal.top); ta.setDuration(200); ta.setInterpolator(new OvershootInterpolator()); inner.startAnimation(ta); //回到正常的布局位置 inner.layout(normal.left, normal.top, normal.right, normal.bottom); normal.setEmpty(); } // 是否需要开启动画 public boolean isNeedAnimation() { return !normal.isEmpty(); } // 是否需要移动布局 public boolean isNeedMove() { //getMeasuredHeight是view的真实高度,即包含不可见部分的高度,getHeight是在屏幕上显示的高度(可见部分) int offset = inner.getMeasuredHeight() - getHeight(); int scrollY = getScrollY(); if (scrollY == 0 || scrollY == offset) {//处在头部或者在底部 return true; } return false; } }布局什么的
<com.onlyway.testandroid.MyScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="300dp" android:scaleType="fitXY" android:src="@mipmap/header" /> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="20dp" android:gravity="center" android:text="苟" android:textColor="#000000" android:textStyle="bold" /> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="利" android:textColor="#000000" android:textStyle="bold"/> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="国" android:textColor="#000000" android:textStyle="bold"/> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="家" android:textColor="#000000" android:textStyle="bold"/> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="生" android:textColor="#000000" android:textStyle="bold"/> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="死" android:textColor="#000000" android:textStyle="bold"/> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="以" android:textColor="#000000" android:textStyle="bold"/> </LinearLayout> </com.onlyway.testandroid.MyScrollView>首秀有人愿意捧场就太好不过了。