标签:patch math 变量 csdn 界面 http mask att 位置
最新项目中有展示类似淘宝商品详情的功能,主要实现 向上拉查看详情,百度一搜,发现有大神已经实现这个效果了
http://blog.csdn.net/zhongkejingwang/article/details/38656929
写的很棒。用2个ScrollView 实现上拉查看详情的功能
可是发如今用在我的项目有点问题,有些小的需求没有实现,所以就在这位大神的基础上加入了简单的功能
1. 事件回调。 第二页全然显示出来的回调,能够用来载入数据
2. 事件回调。 当用户滑到第一个ScrollView底部的时候,继续上拉的时候,跟用户提 ------- 继续上拉。载入很多其它
3. 事件回调, 当用户滑到第一个ScrollView底部的时候,继续上拉之后,在放开屏幕。第一个ScrollView回滚回原先的位置,这是须要改动提示----- 上拉,能够载入很多其它
4. 需改之前代码可能出现错误的地方
5. 添加简单的凝视
改动后的代码例如以下:
import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.View; import android.widget.RelativeLayout; import android.widget.ScrollView; import java.util.Timer; import java.util.TimerTask; public class ScrollViewContainer extends RelativeLayout { /** * 自己主动上滑 */ public static final int AUTO_UP = 0; /** * 自己主动下滑 */ public static final int AUTO_DOWN = 1; /** * 动画完毕 */ public static final int DONE = 2; /** * 动画速度 */ public static final float SPEED = 10.0f; private boolean isMeasured = false; /** * 用于计算手滑动的速度 */ private VelocityTracker vt; private int mViewHeight; private int mViewWidth; private View topView; private View bottomView; private boolean canPullDown; private boolean canPullUp; private int state = DONE; /** * 记录当前展示的是哪个view,0是topView。1是bottomView */ private int mCurrentViewIndex = 0; /** * 手滑动距离。这个是控制布局的主要变量 */ private float mMoveLen; private MyTimer mTimer; private float mLastY; /** * 用于控制是否变动布局的还有一个条件。mEvents==0时布局能够拖拽了,mEvents==-1时能够舍弃将要到来的第一个move事件, * 这点是去除多点拖动剧变的关键 */ private int mEvents; //处理手指离开屏幕后的自己主动滚动,棒棒的写法 private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (mMoveLen != 0) { if (state == AUTO_UP) { mMoveLen -= SPEED; if (mMoveLen <= -mViewHeight) { mMoveLen = -mViewHeight; state = DONE; mCurrentViewIndex = 1; if (l != null) { l.onSecondViewShow(); } } } else if (state == AUTO_DOWN) { mMoveLen += SPEED; if (mMoveLen >= 0) { mMoveLen = 0; state = DONE; mCurrentViewIndex = 0; if (upl != null) { upl.onUpPullHintListener(upl.STATE_DWON); } } } else { mTimer.cancel(); } } //计算组件显示位置 requestLayout(); } }; public ScrollViewContainer(Context context) { super(context); init(); } public ScrollViewContainer(Context context, AttributeSet attrs) { super(context, attrs); init(); } public ScrollViewContainer(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { mTimer = new MyTimer(handler); } /** * 重点 * 处理滑动事件 * 重点 */ @Override public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getActionMasked()) { case MotionEvent.ACTION_DOWN: if (vt == null) vt = VelocityTracker.obtain(); else vt.clear(); mLastY = ev.getY(); vt.addMovement(ev); mEvents = 0; break; case MotionEvent.ACTION_POINTER_DOWN: case MotionEvent.ACTION_POINTER_UP: // 多一仅仅手指按下或抬起时舍弃将要到来的第一个事件move,防止多点拖拽的bug mEvents = -1; break; case MotionEvent.ACTION_MOVE: vt.addMovement(ev); if (canPullUp && mCurrentViewIndex == 0 && mEvents > 0) { mMoveLen += (ev.getY() - mLastY); // 防止上下越界 if (mMoveLen > 0) { mMoveLen = 0; mCurrentViewIndex = 0; } else if (mMoveLen < -mViewHeight / 5) { mMoveLen = -mViewHeight / 5; mCurrentViewIndex = 1; if (upl != null) { upl.onUpPullHintListener(upl.STATE_UP); } } if (mMoveLen < -8) { // 防止事件冲突 ev.setAction(MotionEvent.ACTION_CANCEL); } } else if (canPullDown && mCurrentViewIndex == 1 && mEvents == 0) { mMoveLen += (ev.getY() - mLastY); // 防止上下越界 if (mMoveLen < -mViewHeight) { mMoveLen = -mViewHeight; mCurrentViewIndex = 1; } else if (mMoveLen > 0) { mMoveLen = 0; mCurrentViewIndex = 0; } if (mMoveLen > 8 - mViewHeight) { // 防止事件冲突 ev.setAction(MotionEvent.ACTION_CANCEL); } } else mEvents++; mLastY = ev.getY(); requestLayout(); break; case MotionEvent.ACTION_UP: mLastY = ev.getY(); vt.addMovement(ev); vt.computeCurrentVelocity(700); // 获取Y方向的速度 float mYV = vt.getYVelocity(); if (mMoveLen == 0 || mMoveLen == -mViewHeight) break; if (Math.abs(mYV) < 500) { // 速度小于一定值的时候当作精巧释放,这时候两个View往哪移动取决于滑动的距离 if (mMoveLen <= -mViewHeight / 2) { state = AUTO_UP; } else if (mMoveLen > -mViewHeight / 2) { state = AUTO_DOWN; } } else { // 抬起手指时速度方向决定两个View往哪移动 if (mYV < 0) state = AUTO_UP; else state = AUTO_DOWN; } mTimer.schedule(2); break; } try { super.dispatchTouchEvent(ev); } catch (Exception e) { e.printStackTrace(); } return true; } //计算组件显示的位置 mMoveLen为关键 @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { topView.layout(0, (int) mMoveLen, mViewWidth, topView.getMeasuredHeight() + (int) mMoveLen); bottomView.layout(0, topView.getMeasuredHeight() + (int) mMoveLen, mViewWidth, topView.getMeasuredHeight() + (int) mMoveLen + bottomView.getMeasuredHeight()); } //得到组件的宽高 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (!isMeasured) { isMeasured = true; mViewHeight = getMeasuredHeight(); mViewWidth = getMeasuredWidth(); topView = getChildAt(0); bottomView = getChildAt(1); bottomView.setOnTouchListener(bottomViewTouchListener); topView.setOnTouchListener(topViewTouchListener); } } private OnTouchListener topViewTouchListener = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ScrollView sv = (ScrollView) v; //滑动到第一页的底部 处理事件 if (sv.getScrollY() == (sv.getChildAt(0).getMeasuredHeight() - sv .getMeasuredHeight()) && mCurrentViewIndex == 0) canPullUp = true; else canPullUp = false; return false; } }; private OnTouchListener bottomViewTouchListener = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ScrollView sv = (ScrollView) v; //滑动到第二页的顶部 处理事件 if (sv.getScrollY() == 0 && mCurrentViewIndex == 1) canPullDown = true; else canPullDown = false; return false; } }; class MyTimer { private Handler handler; private Timer timer; private MyTask mTask; public MyTimer(Handler handler) { this.handler = handler; timer = new Timer(); } public void schedule(long period) { if (mTask != null) { mTask.cancel(); mTask = null; } mTask = new MyTask(handler); timer.schedule(mTask, 0, period); } public void cancel() { if (mTask != null) { mTask.cancel(); mTask = null; } } class MyTask extends TimerTask { private Handler handler; public MyTask(Handler handler) { this.handler = handler; } @Override public void run() { handler.obtainMessage().sendToTarget(); } } } private OnUpPullHintListener upl; private OnSecondViewShowListener l; public void setOnSecondViewShowListener(OnSecondViewShowListener l) { this.l = l; } public void setOnUpPullHintListener(OnUpPullHintListener upl) { this.upl = upl; } /** * 上拉提示 */ public interface OnUpPullHintListener { public static final int STATE_UP = 1; //上拉提示 public static final int STATE_DWON = 2; //滚回提示 public void onUpPullHintListener(int state); } /** * 显示第二页的回调 */ public interface OnSecondViewShowListener { public void onSecondViewShow(); } }
使用案例
http://download.csdn.net/detail/mr_wanggang/8770745
标签:patch math 变量 csdn 界面 http mask att 位置
原文地址:http://www.cnblogs.com/jzssuanfa/p/6722087.html