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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
| public class ParentRecyclerView extends RecyclerView {
private final int mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
private int mVelocity = 0;
private float mLastTouchY = 0f;
private int mLastInterceptX; private int mLastInterceptY;
private final VelocityTracker mVelocityTracker = VelocityTracker.obtain(); private int mMaximumFlingVelocity; private int mMinimumFlingVelocity;
private boolean childConsumeTouch = false;
private int childConsumeDistance = 0;
public ParentRecyclerView(@NonNull Context context) { this(context, null); }
public ParentRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); }
public ParentRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); }
private void init() { ViewConfiguration configuration = ViewConfiguration.get(getContext()); mMaximumFlingVelocity = configuration.getScaledMaximumFlingVelocity(); mMinimumFlingVelocity = configuration.getScaledMinimumFlingVelocity();
addOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == SCROLL_STATE_IDLE) { dispatchChildFling(); } } }); }
@Override public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mVelocity = 0; mLastTouchY = ev.getRawY(); childConsumeTouch = false; childConsumeDistance = 0;
ChildRecyclerView childRecyclerView = findNestedScrollingChildRecyclerView(); if (isScrollToBottom() && (childRecyclerView != null && !childRecyclerView.isScrollToTop())) { stopScroll(); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: childConsumeTouch = false; childConsumeDistance = 0; break; default: break; }
try { return super.dispatchTouchEvent(ev); } catch (Exception e) { e.printStackTrace(); return false; } }
@Override public boolean onInterceptTouchEvent(MotionEvent event) { if (isChildConsumeTouch(event)) { return false; } return super.onInterceptTouchEvent(event); }
private boolean isChildConsumeTouch(MotionEvent event) { int x = (int) event.getRawX(); int y = (int) event.getRawY(); if (event.getAction() != MotionEvent.ACTION_MOVE) { mLastInterceptX = x; mLastInterceptY = y; return false; } int deltaX = x - mLastInterceptX; int deltaY = y - mLastInterceptY; if (Math.abs(deltaX) > Math.abs(deltaY) || Math.abs(deltaY) <= mTouchSlop) { return false; }
return shouldChildScroll(deltaY); }
private boolean shouldChildScroll(int deltaY) { ChildRecyclerView childRecyclerView = findNestedScrollingChildRecyclerView(); if (childRecyclerView == null) { return false; } if (isScrollToBottom()) { return deltaY < 0 && !childRecyclerView.isScrollToBottom(); } else { return deltaY > 0 && !childRecyclerView.isScrollToTop(); } }
@Override public boolean onTouchEvent(MotionEvent e) { if (isScrollToBottom()) { ChildRecyclerView childRecyclerView = findNestedScrollingChildRecyclerView(); if (childRecyclerView != null) { int deltaY = (int) (mLastTouchY - e.getRawY()); if (deltaY >= 0 || !childRecyclerView.isScrollToTop()) { mVelocityTracker.addMovement(e); if (e.getAction() == MotionEvent.ACTION_UP) { mVelocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity); float velocityY = mVelocityTracker.getYVelocity(); if (Math.abs(velocityY) > mMinimumFlingVelocity) { childRecyclerView.fling(0, -(int) velocityY); } mVelocityTracker.clear(); } else { childRecyclerView.scrollBy(0, deltaY); }
childConsumeDistance += deltaY; mLastTouchY = e.getRawY(); childConsumeTouch = true; return true; } } }
mLastTouchY = e.getRawY();
if (childConsumeTouch) { MotionEvent adjustedEvent = MotionEvent.obtain( e.getDownTime(), e.getEventTime(), e.getAction(), e.getX(), e.getY() + childConsumeDistance, e.getMetaState() );
boolean handled = super.onTouchEvent(adjustedEvent); adjustedEvent.recycle(); return handled; }
if (e.getAction() == MotionEvent.ACTION_UP || e.getAction() == MotionEvent.ACTION_CANCEL) { mVelocityTracker.clear(); }
try { return super.onTouchEvent(e); } catch (Exception ex) { ex.printStackTrace(); return false; } }
@Override public boolean fling(int velX, int velY) { boolean fling = super.fling(velX, velY); if (!fling || velY <= 0) { mVelocity = 0; } else { mVelocity = velY; } return fling; }
private void dispatchChildFling() { if (isScrollToBottom() && mVelocity != 0) { float mVelocity = NestedOverScroller.invokeCurrentVelocity(this); if (Math.abs(mVelocity) <= 2.0E-5F) { mVelocity = (float) this.mVelocity * 0.5F; } else { mVelocity *= 0.46F; } ChildRecyclerView childRecyclerView = findNestedScrollingChildRecyclerView(); if (childRecyclerView != null) { childRecyclerView.fling(0, (int) mVelocity); } } mVelocity = 0; }
public ChildRecyclerView findNestedScrollingChildRecyclerView() { if (getAdapter() instanceof INestedParentAdapter) { return ((INestedParentAdapter) getAdapter()).getCurrentChildRecyclerView(); } return null; }
public boolean isScrollToBottom() { return !canScrollVertically(1); }
public boolean isScrollToTop() { return !canScrollVertically(-1); }
@Override public void scrollToPosition(final int position) { if (position == 0) { ChildRecyclerView childRecyclerView = findNestedScrollingChildRecyclerView(); if (childRecyclerView != null) { childRecyclerView.scrollToPosition(0); } }
super.scrollToPosition(position); }
@Override public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) { return target instanceof ChildRecyclerView; }
@Override public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) { ChildRecyclerView childRecyclerView = findNestedScrollingChildRecyclerView(); boolean isParentCanScroll = dy > 0 && !isScrollToBottom(); boolean isChildCanNotScroll = dy < 0 && (childRecyclerView == null || childRecyclerView.isScrollToTop()); if (isParentCanScroll || isChildCanNotScroll) { smoothScrollBy(0, dy); consumed[1] = dy; } }
@Override public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) { return true; }
@Override public boolean onNestedPreFling(View target, float velocityX, float velocityY) { ChildRecyclerView childRecyclerView = findNestedScrollingChildRecyclerView(); boolean isParentCanFling = velocityY > 0f && !isScrollToBottom(); boolean isChildCanNotFling = velocityY < 0 && (childRecyclerView == null || childRecyclerView.isScrollToTop());
if (!isParentCanFling && !isChildCanNotFling) { return false; } fling(0, (int) velocityY); return true; } }
|