|
| 1 | +package razerdp.friendcircle.widget.pullrecyclerview; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Color; |
| 5 | +import android.graphics.Point; |
| 6 | +import android.graphics.PointF; |
| 7 | +import android.support.annotation.IntDef; |
| 8 | +import android.support.v7.widget.LinearLayoutManager; |
| 9 | +import android.support.v7.widget.RecyclerView; |
| 10 | +import android.util.AttributeSet; |
| 11 | +import android.util.Log; |
| 12 | +import android.view.MotionEvent; |
| 13 | +import android.view.View; |
| 14 | +import android.view.ViewConfiguration; |
| 15 | +import android.widget.LinearLayout; |
| 16 | + |
| 17 | +import com.socks.library.KLog; |
| 18 | + |
| 19 | +import java.lang.annotation.Retention; |
| 20 | +import java.lang.annotation.RetentionPolicy; |
| 21 | + |
| 22 | +import razerdp.friendcircle.widget.pullrecyclerview.interfaces.CirclePtrRefreshView; |
| 23 | +import razerdp.friendcircle.widget.pullrecyclerview.interfaces.OnRefreshListener2; |
| 24 | +import razerdp.friendcircle.widget.pullrecyclerview.interfaces.PtrState; |
| 25 | + |
| 26 | +import static razerdp.friendcircle.widget.pullrecyclerview.CircleRecyclerView.Status.*; |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * Created by 大灯泡 on 2016/10/29. |
| 31 | + * <p> |
| 32 | + * 专为朋友圈项目定制的下拉recyclerview |
| 33 | + * 原因在于: |
| 34 | + * 1 - 目前而言,git上大多数的rv都是用的swiperefreshlayout |
| 35 | + * 2 - 大多数rv都不支持下拉后收回下拉头部的 |
| 36 | + * 3 - 因为大多数的rv都支持太多功能了,显得有点重,并且有些想要的回调我们都没法拿到 |
| 37 | + * <p> |
| 38 | + * 综上所述,干脆自己弄一个算了。。。。 |
| 39 | + * <p> |
| 40 | + * 目标: |
| 41 | + * 【基本要求】因为相当于定制,只为本项目服务,因此不考虑通用性,更多考虑扩展性 |
| 42 | + * <p> |
| 43 | + * 1 - 下拉和上拉支持回调 |
| 44 | + * 2 - 跟iOS朋友圈下拉头部一样,支持头部在刷新时的回弹 |
| 45 | + * 3 - 滑动到底部自动加载更多 |
| 46 | + * 4 - addHeaderView。 |
| 47 | + */ |
| 48 | + |
| 49 | +public class CircleRecyclerView extends LinearLayout { |
| 50 | + public static boolean DEBUG = true; |
| 51 | + private static final String TAG = "CircleRecyclerView"; |
| 52 | + |
| 53 | + @Retention(RetentionPolicy.SOURCE) |
| 54 | + @IntDef({DEFAULT, PULL_TO_REFRESH, REFRESHING}) |
| 55 | + @interface Status { |
| 56 | + int DEFAULT = 0; |
| 57 | + int PULL_TO_REFRESH = 1; |
| 58 | + int REFRESHING = 2; |
| 59 | + } |
| 60 | + |
| 61 | + @Status |
| 62 | + private int currentStatus; |
| 63 | + |
| 64 | + //observer |
| 65 | + private InnerRefreshViewObserver refreshViewObserver; |
| 66 | + private InnerMotionEventObserver motionEventObserver; |
| 67 | + |
| 68 | + //callback |
| 69 | + private OnRefreshListener2 onRefreshListener; |
| 70 | + |
| 71 | + //view |
| 72 | + private CirclePtrRefreshView mPtrHeaderView; |
| 73 | + private CirclePtrRefreshView mPtrFooterView; |
| 74 | + private RecyclerView recyclerView; |
| 75 | + |
| 76 | + //options |
| 77 | + private int positionForRefresh; |
| 78 | + private boolean canPull = true; |
| 79 | + |
| 80 | + |
| 81 | + public CircleRecyclerView(Context context) { |
| 82 | + this(context, null); |
| 83 | + } |
| 84 | + |
| 85 | + public CircleRecyclerView(Context context, AttributeSet attrs) { |
| 86 | + this(context, attrs, 0); |
| 87 | + } |
| 88 | + |
| 89 | + public CircleRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 90 | + super(context, attrs, defStyleAttr); |
| 91 | + init(context); |
| 92 | + } |
| 93 | + |
| 94 | + private void init(Context context) { |
| 95 | + setOrientation(VERTICAL); |
| 96 | + setBackgroundColor(Color.TRANSPARENT); |
| 97 | + |
| 98 | + refreshViewObserver = new InnerRefreshViewObserver(); |
| 99 | + motionEventObserver = new InnerMotionEventObserver(context); |
| 100 | + |
| 101 | + if (mPtrHeaderView == null) { |
| 102 | + mPtrHeaderView = new CirclePtrHeaderView(context); |
| 103 | + } |
| 104 | + if (mPtrFooterView == null) { |
| 105 | + mPtrFooterView = new CirclePtrFooterView(context); |
| 106 | + } |
| 107 | + if (recyclerView == null) { |
| 108 | + recyclerView = new RecyclerView(context); |
| 109 | + recyclerView.setLayoutManager(new LinearLayoutManager(context, VERTICAL, false)); |
| 110 | + } |
| 111 | + addView((View) mPtrHeaderView); |
| 112 | + addView(recyclerView); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + protected void onFinishInflate() { |
| 117 | + if (getChildCount() > 2) { |
| 118 | + throw new IllegalStateException("超过了两个子view哦"); |
| 119 | + } |
| 120 | + super.onFinishInflate(); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 125 | + checkRefreshViewValided(); |
| 126 | + super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 127 | + |
| 128 | + if (mPtrHeaderView != null) { |
| 129 | + measureChildWithMargins((View) mPtrHeaderView, widthMeasureSpec, 0, heightMeasureSpec, 0); |
| 130 | + MarginLayoutParams ptrHeaderLayoutParams = (MarginLayoutParams) ((View) mPtrHeaderView).getLayoutParams(); |
| 131 | + int ptrHeaderWidth = ((View) mPtrHeaderView).getMeasuredWidth() + ptrHeaderLayoutParams.leftMargin + ptrHeaderLayoutParams.rightMargin; |
| 132 | + int ptrHeaderHeight = ((View) mPtrHeaderView).getMeasuredHeight() + ptrHeaderLayoutParams.topMargin + ptrHeaderLayoutParams.bottomMargin; |
| 133 | + refreshViewObserver.setPtrHeaderWidth(ptrHeaderWidth); |
| 134 | + refreshViewObserver.setPtrHeaderHeight(ptrHeaderHeight); |
| 135 | + } |
| 136 | + |
| 137 | + /* if (mPtrFooterView != null) { |
| 138 | + measureChildWithMargins((View) mPtrFooterView, widthMeasureSpec, 0, heightMeasureSpec, 0); |
| 139 | + MarginLayoutParams ptrFooterLayoutParams = (MarginLayoutParams) ((View) mPtrFooterView).getLayoutParams(); |
| 140 | + int ptrFooterWidth = ((View) mPtrFooterView).getMeasuredWidth() + ptrFooterLayoutParams.leftMargin + ptrFooterLayoutParams.rightMargin; |
| 141 | + int ptrFooterHeight = ((View) mPtrFooterView).getMeasuredHeight() + ptrFooterLayoutParams.topMargin + ptrFooterLayoutParams.bottomMargin; |
| 142 | + refreshViewObserver.setPtrFooterWidth(ptrFooterWidth); |
| 143 | + refreshViewObserver.setPtrFooterHeight(ptrFooterHeight); |
| 144 | + }*/ |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 149 | + if (mPtrHeaderView != null) { |
| 150 | + final int left = 0; |
| 151 | + final int top = -refreshViewObserver.getPtrHeaderHeight() + motionEventObserver.getCurrentY(); |
| 152 | + final int right = left + refreshViewObserver.getPtrHeaderWidth(); |
| 153 | + final int bottom = top + refreshViewObserver.getPtrHeaderHeight(); |
| 154 | + //摆放header的位置,受滑动距离影响,设为负值让其隐藏 |
| 155 | + ((View) mPtrHeaderView).layout(left, top, right, bottom); |
| 156 | + } |
| 157 | + super.onLayout(changed, l, t, r, b); |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + private void setCurrentStatus(@Status int status) { |
| 162 | + this.currentStatus = status; |
| 163 | + |
| 164 | + } |
| 165 | + |
| 166 | + private void checkRefreshViewValided() { |
| 167 | + if (mPtrHeaderView != null && !(mPtrHeaderView instanceof View)) { |
| 168 | + throw new IllegalStateException("诶多。。。那个。。。接口都有个View字哦,请给一个View来-V-"); |
| 169 | + } |
| 170 | + if (mPtrFooterView != null && !(mPtrFooterView instanceof View)) { |
| 171 | + throw new IllegalStateException("诶多。。。那个。。。接口都有个View字哦,请给一个View来-V-"); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public boolean dispatchTouchEvent(MotionEvent ev) { |
| 177 | + if (!isCanPull()) { |
| 178 | + return super.dispatchTouchEvent(ev); |
| 179 | + } else { |
| 180 | + switch (ev.getAction()) { |
| 181 | + case MotionEvent.ACTION_DOWN: |
| 182 | + motionEventObserver.catchTouchDown(ev.getX(), ev.getY()); |
| 183 | + super.dispatchTouchEvent(ev); |
| 184 | + return true; |
| 185 | + case MotionEvent.ACTION_MOVE: |
| 186 | + motionEventObserver.catchTouchMove(ev.getX(), ev.getY()); |
| 187 | + float offsetX = motionEventObserver.getOffsetX(); |
| 188 | + float offsetY = motionEventObserver.getOffsetY(); |
| 189 | + //横向移动比纵向大,则传递下去 |
| 190 | + if (Math.abs(offsetX) > Math.abs(offsetY * 2)) { |
| 191 | + if (DEBUG) { |
| 192 | + Log.d(TAG, "横向移动,取消事件"); |
| 193 | + } |
| 194 | + return super.dispatchTouchEvent(ev); |
| 195 | + } |
| 196 | + |
| 197 | + if (motionEventObserver.isDrag()) { |
| 198 | + if (motionEventObserver.isValidTouchSlop(offsetY)) { |
| 199 | + doMove(ev); |
| 200 | + return true; |
| 201 | + } |
| 202 | + } |
| 203 | + break; |
| 204 | + } |
| 205 | + } |
| 206 | + return super.dispatchTouchEvent(ev); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * 处理移动事件 |
| 211 | + */ |
| 212 | + private void doMove(MotionEvent ev) { |
| 213 | + |
| 214 | + //如果是下拉 |
| 215 | + if (motionEventObserver.isFromTop()) { |
| 216 | + mPtrHeaderView.onStart(PtrState.START_TO_PULL, positionForRefresh); |
| 217 | + } |
| 218 | + //刷新位置 |
| 219 | + int currentY = (int) (motionEventObserver.getCurrentY() + motionEventObserver.getOffsetY()); |
| 220 | + Log.i(TAG, ">>>isPullDown: " + motionEventObserver.isPullDown() + "<<< currentY = " + currentY); |
| 221 | + motionEventObserver.setCurrentY(currentY); |
| 222 | + if (currentY < positionForRefresh) { |
| 223 | + mPtrHeaderView.onMoved(PtrState.PULL_TO_REFRESH, positionForRefresh, (int) motionEventObserver.getOffsetY()); |
| 224 | + } else { |
| 225 | + mPtrHeaderView.onMoved(PtrState.RELEASE_TO_REFRESH, positionForRefresh, (int) motionEventObserver.getOffsetY()); |
| 226 | + } |
| 227 | + ((View) mPtrHeaderView).offsetTopAndBottom((int) motionEventObserver.getOffsetY()); |
| 228 | + recyclerView.offsetTopAndBottom((int) motionEventObserver.getOffsetY()); |
| 229 | + |
| 230 | + } |
| 231 | + |
| 232 | + //------------------------------------------get/set----------------------------------------------- |
| 233 | + |
| 234 | + public OnRefreshListener2 getOnRefreshListener() { |
| 235 | + return onRefreshListener; |
| 236 | + } |
| 237 | + |
| 238 | + public void setOnRefreshListener(OnRefreshListener2 onRefreshListener) { |
| 239 | + this.onRefreshListener = onRefreshListener; |
| 240 | + } |
| 241 | + |
| 242 | + public boolean isCanPull() { |
| 243 | + return canPull; |
| 244 | + } |
| 245 | + |
| 246 | + public void setCanPull(boolean canPull) { |
| 247 | + this.canPull = canPull; |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * ============分割线=============== |
| 252 | + * 各种状态的观察 |
| 253 | + * ============分割线=============== |
| 254 | + */ |
| 255 | + |
| 256 | + /** |
| 257 | + * 刷新View的观察者 |
| 258 | + */ |
| 259 | + private static class InnerRefreshViewObserver { |
| 260 | + private static final String INNERTAG = "InnerRefreshViewObserve"; |
| 261 | + private int ptrHeaderWidth; |
| 262 | + private int ptrHeaderHeight; |
| 263 | + private int ptrFooterWidth; |
| 264 | + private int ptrFooterHeight; |
| 265 | + |
| 266 | + InnerRefreshViewObserver() { |
| 267 | + } |
| 268 | + |
| 269 | + int getPtrHeaderWidth() { |
| 270 | + return ptrHeaderWidth; |
| 271 | + } |
| 272 | + |
| 273 | + void setPtrHeaderWidth(int ptrHeaderWidth) { |
| 274 | + this.ptrHeaderWidth = ptrHeaderWidth; |
| 275 | + if (DEBUG) { |
| 276 | + Log.i(INNERTAG, "ptrHeaderWidth >>> " + ptrHeaderWidth); |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + int getPtrHeaderHeight() { |
| 281 | + return ptrHeaderHeight; |
| 282 | + } |
| 283 | + |
| 284 | + void setPtrHeaderHeight(int ptrHeaderHeight) { |
| 285 | + this.ptrHeaderHeight = ptrHeaderHeight; |
| 286 | + if (DEBUG) { |
| 287 | + Log.i(INNERTAG, "ptrHeaderHeight >>> " + ptrHeaderWidth); |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + int getPtrFooterWidth() { |
| 292 | + return ptrFooterWidth; |
| 293 | + } |
| 294 | + |
| 295 | + void setPtrFooterWidth(int ptrFooterWidth) { |
| 296 | + this.ptrFooterWidth = ptrFooterWidth; |
| 297 | + if (DEBUG) { |
| 298 | + Log.i(INNERTAG, "ptrFooterWidth >>> " + ptrHeaderWidth); |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + int getPtrFooterHeight() { |
| 303 | + return ptrFooterHeight; |
| 304 | + } |
| 305 | + |
| 306 | + void setPtrFooterHeight(int ptrFooterHeight) { |
| 307 | + this.ptrFooterHeight = ptrFooterHeight; |
| 308 | + if (DEBUG) { |
| 309 | + Log.i(INNERTAG, "ptrFooterHeight >>> " + ptrHeaderWidth); |
| 310 | + } |
| 311 | + } |
| 312 | + } |
| 313 | + |
| 314 | + /** |
| 315 | + * 手势动作的观察者 |
| 316 | + */ |
| 317 | + private static class InnerMotionEventObserver { |
| 318 | + //是否正在拖动拖动 |
| 319 | + private boolean isDrag; |
| 320 | + //是否从初始状态拖动 |
| 321 | + private boolean isFirstDrag; |
| 322 | + //是否到达顶部 |
| 323 | + private boolean returnToTop; |
| 324 | + //上一次的位置 |
| 325 | + private PointF lastPos; |
| 326 | + |
| 327 | + private int currentY; |
| 328 | + |
| 329 | + //位移量 |
| 330 | + private float offsetX; |
| 331 | + private float offsetY; |
| 332 | + |
| 333 | + //最小滑动判定值 |
| 334 | + private int minTouchSlop; |
| 335 | + |
| 336 | + InnerMotionEventObserver(Context context) { |
| 337 | + lastPos = new PointF(); |
| 338 | + ViewConfiguration conf = ViewConfiguration.get(context); |
| 339 | + minTouchSlop = conf.getScaledTouchSlop(); |
| 340 | + } |
| 341 | + |
| 342 | + void catchTouchDown(float x, float y) { |
| 343 | + isDrag = true; |
| 344 | + lastPos.set(x, y); |
| 345 | + } |
| 346 | + |
| 347 | + void catchTouchMove(float x, float y) { |
| 348 | + this.offsetX = x - lastPos.x; |
| 349 | + //阻尼1.2 |
| 350 | + this.offsetY = (y - lastPos.y) / 1.2f; |
| 351 | + lastPos.set(x, y); |
| 352 | + } |
| 353 | + |
| 354 | + boolean isDrag() { |
| 355 | + return isDrag; |
| 356 | + } |
| 357 | + |
| 358 | + void setDrag(boolean drag) { |
| 359 | + isDrag = drag; |
| 360 | + } |
| 361 | + |
| 362 | + boolean isFirstDrag() { |
| 363 | + return isFirstDrag; |
| 364 | + } |
| 365 | + |
| 366 | + void setFirstDrag(boolean firstDrag) { |
| 367 | + isFirstDrag = firstDrag; |
| 368 | + } |
| 369 | + |
| 370 | + float getOffsetX() { |
| 371 | + return offsetX; |
| 372 | + } |
| 373 | + |
| 374 | + float getOffsetY() { |
| 375 | + return offsetY; |
| 376 | + } |
| 377 | + |
| 378 | + int getCurrentY() { |
| 379 | + return currentY < 0 ? 0 : currentY; |
| 380 | + } |
| 381 | + |
| 382 | + void setCurrentY(int currentY) { |
| 383 | + this.currentY = currentY; |
| 384 | + returnToTop = currentY <= 0; |
| 385 | + } |
| 386 | + |
| 387 | + boolean isValidTouchSlop(float offset) { |
| 388 | + return Math.abs(offset) >= minTouchSlop; |
| 389 | + } |
| 390 | + |
| 391 | + boolean isPullDown() { |
| 392 | + return offsetY > 0; |
| 393 | + } |
| 394 | + |
| 395 | + boolean isFromTop() { |
| 396 | + return currentY == 0; |
| 397 | + } |
| 398 | + } |
| 399 | +} |
0 commit comments