1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| package com.example.work3;
import android.content.Context; import android.support.v4.view.ViewConfigurationCompat; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; import android.widget.Scroller;
public class ScrollLayout extends ViewGroup {
private final String TAG = "test_scroll"; private Scroller mScroller; private int mTouchSlop; private float mXDown; private float mXMove; private float mLastMove; private int mLeftBorder; private int mRightBorder;
public ScrollLayout(Context context) { super(context); init(context); }
public ScrollLayout(Context context, AttributeSet attrs) { super(context, attrs); init(context); }
public ScrollLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); }
private void init(Context context) { mScroller = new Scroller(context); mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(ViewConfiguration.get(context)); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec); } }
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { if (!changed) return; int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View childView = getChildAt(i); childView.layout(i * childView.getMeasuredWidth(), 0, (i + 1) * childView.getMeasuredWidth(), childView.getMeasuredHeight()); } mLeftBorder = getChildAt(0).getLeft(); mRightBorder = getChildAt(childCount - 1).getRight(); }
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mXDown = ev.getRawX(); mLastMove = mXDown; break; case MotionEvent.ACTION_MOVE: mXMove = ev.getRawX(); float diff = Math.abs(mXMove - mXDown); mLastMove = mXMove; if (diff > mTouchSlop) { return true; } break; case MotionEvent.ACTION_UP: break; default: break; } return super.onInterceptTouchEvent(ev); }
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: { mXMove = event.getRawX(); int scrolledX = (int) (mLastMove - mXMove); if (getScrollX() + scrolledX < mLeftBorder) { scrollTo(mLeftBorder, 0); return true; } else if (getScrollX() + scrolledX + getWidth() > mRightBorder) { scrollTo(mRightBorder - getWidth(), 0); return true; } scrollBy(scrolledX, 0); Log.i(TAG, "onTouchEvent: " + getChildAt(1).getLeft()); mLastMove = mXMove; break; } case MotionEvent.ACTION_UP: { int targetIndex = (getScrollX() + getWidth() / 2) / getWidth(); int dx = targetIndex * getWidth() - getScrollX(); mScroller.startScroll(getScrollX(), 0, dx, 0); invalidate(); break; } } return super.onTouchEvent(event); }
@Override public void computeScroll() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); invalidate(); } } }
|