-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from AlanCheen/feature/sticky-header
Feature/sticky header
- Loading branch information
Showing
16 changed files
with
1,676 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
app/src/main/java/me/yifeiyuan/flapdev/SingleScrollDirection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package me.yifeiyuan.flapdev | ||
|
||
import android.view.MotionEvent | ||
import androidx.recyclerview.widget.RecyclerView | ||
import java.lang.Math.abs | ||
|
||
/** | ||
* Created by 程序亦非猿 on 2022/10/18. | ||
*/ | ||
|
||
fun RecyclerView.enforceSingleScrollDirection() { | ||
val enforcer = SingleScrollDirectionEnforcer() | ||
addOnItemTouchListener(enforcer) | ||
addOnScrollListener(enforcer) | ||
} | ||
|
||
private class SingleScrollDirectionEnforcer : RecyclerView.OnScrollListener(), RecyclerView.OnItemTouchListener { | ||
|
||
private var scrollState = RecyclerView.SCROLL_STATE_IDLE | ||
private var scrollPointerId = -1 | ||
private var initialTouchX = 0 | ||
private var initialTouchY = 0 | ||
private var dx = 0 | ||
private var dy = 0 | ||
|
||
override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean { | ||
when (e.actionMasked) { | ||
MotionEvent.ACTION_DOWN -> { | ||
scrollPointerId = e.getPointerId(0) | ||
initialTouchX = (e.x + 0.5f).toInt() | ||
initialTouchY = (e.y + 0.5f).toInt() | ||
} | ||
MotionEvent.ACTION_POINTER_DOWN -> { | ||
val actionIndex = e.actionIndex | ||
scrollPointerId = e.getPointerId(actionIndex) | ||
initialTouchX = (e.getX(actionIndex) + 0.5f).toInt() | ||
initialTouchY = (e.getY(actionIndex) + 0.5f).toInt() | ||
} | ||
MotionEvent.ACTION_MOVE -> { | ||
val index = e.findPointerIndex(scrollPointerId) | ||
if (index >= 0 && scrollState != RecyclerView.SCROLL_STATE_DRAGGING) { | ||
val x = (e.getX(index) + 0.5f).toInt() | ||
val y = (e.getY(index) + 0.5f).toInt() | ||
dx = x - initialTouchX | ||
dy = y - initialTouchY | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {} | ||
|
||
override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {} | ||
|
||
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { | ||
val oldState = scrollState | ||
scrollState = newState | ||
if (oldState == RecyclerView.SCROLL_STATE_IDLE && newState == RecyclerView.SCROLL_STATE_DRAGGING) { | ||
recyclerView.layoutManager?.let { layoutManager -> | ||
val canScrollHorizontally = layoutManager.canScrollHorizontally() | ||
val canScrollVertically = layoutManager.canScrollVertically() | ||
if (canScrollHorizontally != canScrollVertically) { | ||
if ((canScrollHorizontally && abs(dy) > abs(dx)) | ||
|| (canScrollVertically && abs(dx) > abs(dy))) { | ||
recyclerView.stopScroll() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package me.yifeiyuan.flapdev; | ||
|
||
import android.view.View; | ||
|
||
/** | ||
* Created by 程序亦非猿 on 2022/10/17. | ||
*/ | ||
interface StickyHeaders { | ||
|
||
boolean isStickyHeader(int position); | ||
|
||
interface ViewSetup { | ||
/** | ||
* Adjusts any necessary properties of the {@code holder} that is being used as a sticky header. | ||
* | ||
* {@link #teardownStickyHeaderView(View)} will be called sometime after this method | ||
* and before any other calls to this method go through. | ||
*/ | ||
void setupStickyHeaderView(View stickyHeader); | ||
|
||
/** | ||
* Reverts any properties changed in {@link #setupStickyHeaderView(View)}. | ||
* | ||
* Called after {@link #setupStickyHeaderView(View)}. | ||
*/ | ||
void teardownStickyHeaderView(View stickyHeader); | ||
} | ||
} |
Oops, something went wrong.