ใน Android โดยปกติแล้วการเลื่อนจะทำได้โดยใช้คลาส ScrollView ให้ซ้อนเลย์เอาต์มาตรฐานใดๆ ที่อาจขยายออกไปนอกขอบเขตของคอนเทนเนอร์ใน ScrollView เพื่อให้มีมุมมองที่เลื่อนได้ซึ่งเฟรมเวิร์กจัดการ คุณจะต้องใช้ตัวเลื่อนที่กำหนดเองในสถานการณ์พิเศษเท่านั้น เอกสารนี้อธิบายวิธีแสดงเอฟเฟกต์การเลื่อนเพื่อตอบสนองต่อท่าทางสัมผัสโดยใช้ ตัวเลื่อน
แอปของคุณสามารถใช้
ตัวเลื่อนอย่างScroller
หรือ
OverScrollerเพื่อ
รวบรวมข้อมูลที่จำเป็นในการสร้างภาพเคลื่อนไหวการเลื่อนเพื่อตอบสนองต่อเหตุการณ์สัมผัส
ตัวเลื่อนทั้ง 2 อย่างนี้มีความคล้ายคลึงกัน แต่ OverScroller ยังมีเมธอดสำหรับระบุให้ผู้ใช้ทราบเมื่อถึงขอบเนื้อหาหลังจากใช้ท่าทางแพนหรือตวัด
- ตั้งแต่ Android 12 (ระดับ API 31) เป็นต้นไป องค์ประกอบภาพจะยืดและเด้ง กลับเมื่อเกิดเหตุการณ์การลาก และสะบัดและเด้งกลับเมื่อเกิดเหตุการณ์สะบัด
- ใน Android 11 (ระดับ API 30) และเวอร์ชันก่อนหน้า ขอบเขตจะแสดงเอฟเฟกต์ "เรืองแสง" หลังจากใช้ท่าทางลากหรือสะบัดไปที่ขอบ
ตัวอย่าง InteractiveChart ในเอกสารนี้ใช้คลาส EdgeEffect เพื่อแสดงเอฟเฟกต์การเลื่อนเลยขอบเหล่านี้
คุณสามารถใช้ตัวเลื่อนเพื่อสร้างภาพเคลื่อนไหวการเลื่อนเมื่อเวลาผ่านไป โดยใช้ฟิสิกส์การเลื่อนมาตรฐานของแพลตฟอร์ม เช่น แรงเสียดทาน อัตราความเร็ว และคุณสมบัติอื่นๆ ตัวเลื่อนเองจะไม่วาดอะไรเลย ตัวเลื่อนจะติดตามออฟเซ็ตการเลื่อนให้คุณเมื่อเวลาผ่านไป แต่จะไม่ใช้ตำแหน่งเหล่านั้นกับมุมมองของคุณโดยอัตโนมัติ คุณต้องรับและใช้พิกัดใหม่ในอัตราที่ทำให้ภาพเคลื่อนไหวการเลื่อนดูราบรื่น
ทำความเข้าใจคำศัพท์เกี่ยวกับการเลื่อน
คำว่า "การเลื่อน" อาจมีความหมายแตกต่างกันใน Android ขึ้นอยู่กับบริบท
การเลื่อนเป็นกระบวนการทั่วไปในการย้ายวิวพอร์ต ซึ่งก็คือ "หน้าต่าง" ของเนื้อหาที่คุณกำลังดู เมื่อการเลื่อนเกิดขึ้นทั้งในแกน
xและ yจะเรียกว่า การแพน แอปตัวอย่าง InteractiveChart ในเอกสารนี้แสดงการเลื่อน 2 ประเภท ได้แก่ การลากและการสะบัด
- การลาก: การเลื่อนประเภทนี้เกิดขึ้นเมื่อผู้ใช้
ลากนิ้วไปบนหน้าจอสัมผัส คุณสามารถติดตั้งใช้งานการลากได้โดย
การลบล้าง
onScroll()ในGestureDetector.OnGestureListener. ดูข้อมูลเพิ่มเติมเกี่ยวกับการลากได้ที่ การลากและการปรับขนาด - การสะบัด: การเลื่อนประเภทนี้เกิดขึ้นเมื่อผู้ใช้
ลากและยกนิ้วขึ้นอย่างรวดเร็ว หลังจากผู้ใช้ยกนิ้วขึ้น โดยทั่วไปแล้วคุณจะต้องการให้วิวพอร์ตเคลื่อนที่ต่อไป แต่ให้ช้าลงจนกว่าวิวพอร์ตจะหยุดเคลื่อนที่ คุณสามารถติดตั้งใช้งานการสะบัดได้โดยการลบล้าง
onFling()ในGestureDetector.OnGestureListenerและใช้ออบเจ็กต์ตัวเลื่อน - การแพน: การเลื่อนไปพร้อมๆ กันตามแกน x และ y เรียกว่า การแพน
โดยทั่วไปแล้วจะใช้ออบเจ็กต์ตัวเลื่อนร่วมกับท่าทางสะบัด แต่คุณสามารถใช้ในบริบทใดก็ได้ที่ต้องการให้ UI แสดงการเลื่อนเพื่อตอบสนองต่อการโต้ตอบแบบสัมผัส ตัวอย่างเช่น คุณสามารถลบล้าง onTouchEvent() เพื่อประมวลผลเหตุการณ์สัมผัสโดยตรงและสร้างเอฟเฟกต์การเลื่อนหรือภาพเคลื่อนไหว "snap-to-page" เพื่อตอบสนองต่อเหตุการณ์สัมผัสเหล่านั้น
คอมโพเนนต์ที่มีการติดตั้งใช้งานการเลื่อนในตัว
คอมโพเนนต์ Android ต่อไปนี้มีการรองรับในตัวสำหรับพฤติกรรมการเลื่อนและการเลื่อนเลยขอบ
GridViewHorizontalScrollViewListViewNestedScrollViewRecyclerViewScrollViewViewPagerViewPager2
หากแอปของคุณต้องรองรับการเลื่อนและการเลื่อนเลยขอบภายในคอมโพเนนต์อื่น ให้ทำตามขั้นตอนต่อไปนี้
- สร้างการติดตั้งใช้งานการเลื่อนแบบอิงตามการสัมผัสที่กำหนดเอง implementation.
- หากต้องการรองรับอุปกรณ์ที่ใช้ Android 12 ขึ้นไป ให้ติดตั้งใช้งานเอฟเฟกต์การเลื่อนเกินแบบยืด
สร้างการติดตั้งใช้งานการเลื่อนแบบอิงตามการสัมผัสที่กำหนดเอง
ข้อมูลโค้ดต่อไปนี้มาจากตัวอย่าง
InteractiveChart โดยใช้
GestureDetector
และลบล้างเมธอด
GestureDetector.SimpleOnGestureListener
onFling() และใช้ OverScroller เพื่อติดตามท่าทางสะบัด หากผู้ใช้ไปถึงขอบเนื้อหาหลังจากใช้ท่าทางสะบัด คอนเทนเนอร์จะระบุเมื่อผู้ใช้ไปถึงจุดสิ้นสุดของเนื้อหา การระบุจะขึ้นอยู่กับเวอร์ชันของ Android ที่อุปกรณ์ใช้
- ใน Android 12 ขึ้นไป องค์ประกอบภาพจะยืดและ เด้งกลับ
- ใน Android 11 และเวอร์ชันก่อนหน้า องค์ประกอบภาพจะแสดงเอฟเฟกต์เรืองแสง
ส่วนแรกของข้อมูลโค้ดต่อไปนี้แสดงการติดตั้งใช้งาน onFling()
Kotlin
// Viewport extremes. See currentViewport for a discussion of the viewport. private val AXIS_X_MIN = -1f private val AXIS_X_MAX = 1f private val AXIS_Y_MIN = -1f private val AXIS_Y_MAX = 1f // The current viewport. This rectangle represents the visible chart // domain and range. The viewport is the part of the app that the // user manipulates via touch gestures. private val currentViewport = RectF(AXIS_X_MIN, AXIS_Y_MIN, AXIS_X_MAX, AXIS_Y_MAX) // The current destination rectangle—in pixel coordinates—into which // the chart data must be drawn. private lateinit var contentRect: Rect private lateinit var scroller: OverScroller private lateinit var scrollerStartViewport: RectF ... private val gestureListener = object : GestureDetector.SimpleOnGestureListener() { override fun onDown(e: MotionEvent): Boolean { // Initiates the decay phase of any active edge effects. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { releaseEdgeEffects() } scrollerStartViewport.set(currentViewport) // Aborts any active scroll animations and invalidates. scroller.forceFinished(true) ViewCompat.postInvalidateOnAnimation(this@InteractiveLineGraphView) return true } ... override fun onFling( e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float ): Boolean { fling((-velocityX).toInt(), (-velocityY).toInt()) return true } } private fun fling(velocityX: Int, velocityY: Int) { // Initiates the decay phase of any active edge effects. // On Android 12 and later, the edge effect (stretch) must // continue. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { releaseEdgeEffects() } // Flings use math in pixels, as opposed to math based on the viewport. val surfaceSize: Point = computeScrollSurfaceSize() val (startX: Int, startY: Int) = scrollerStartViewport.run { set(currentViewport) (surfaceSize.x * (left - AXIS_X_MIN) / (AXIS_X_MAX - AXIS_X_MIN)).toInt() to (surfaceSize.y * (AXIS_Y_MAX - bottom) / (AXIS_Y_MAX - AXIS_Y_MIN)).toInt() } // Before flinging, stops the current animation. scroller.forceFinished(true) // Begins the animation. scroller.fling( // Current scroll position. startX, startY, velocityX, velocityY, /* * Minimum and maximum scroll positions. The minimum scroll * position is generally 0 and the maximum scroll position * is generally the content size less the screen size. So if the * content width is 1000 pixels and the screen width is 200 * pixels, the maximum scroll offset is 800 pixels. */ 0, surfaceSize.x - contentRect.width(), 0, surfaceSize.y - contentRect.height(), // The edges of the content. This comes into play when using // the EdgeEffect class to draw "glow" overlays. contentRect.width() / 2, contentRect.height() / 2 ) // Invalidates to trigger computeScroll(). ViewCompat.postInvalidateOnAnimation(this) }
Java
// Viewport extremes. See currentViewport for a discussion of the viewport. private static final float AXIS_X_MIN = -1f; private static final float AXIS_X_MAX = 1f; private static final float AXIS_Y_MIN = -1f; private static final float AXIS_Y_MAX = 1f; // The current viewport. This rectangle represents the visible chart // domain and range. The viewport is the part of the app that the // user manipulates via touch gestures. private RectF currentViewport = new RectF(AXIS_X_MIN, AXIS_Y_MIN, AXIS_X_MAX, AXIS_Y_MAX); // The current destination rectangle—in pixel coordinates—into which // the chart data must be drawn. private final Rect contentRect = new Rect(); private final OverScroller scroller; private final RectF scrollerStartViewport = new RectF(); // Used only for zooms and flings. ... private final GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() { @Override public boolean onDown(MotionEvent e) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { releaseEdgeEffects(); } scrollerStartViewport.set(currentViewport); scroller.forceFinished(true); ViewCompat.postInvalidateOnAnimation(InteractiveLineGraphView.this); return true; } ... @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { fling((int) -velocityX, (int) -velocityY); return true; } }; private void fling(int velocityX, int velocityY) { // Initiates the decay phase of any active edge effects. // On Android 12 and later, the edge effect (stretch) must // continue. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { releaseEdgeEffects(); } // Flings use math in pixels, as opposed to math based on the viewport. Point surfaceSize = computeScrollSurfaceSize(); scrollerStartViewport.set(currentViewport); int startX = (int) (surfaceSize.x * (scrollerStartViewport.left - AXIS_X_MIN) / ( AXIS_X_MAX - AXIS_X_MIN)); int startY = (int) (surfaceSize.y * (AXIS_Y_MAX - scrollerStartViewport.bottom) / ( AXIS_Y_MAX - AXIS_Y_MIN)); // Before flinging, stops the current animation. scroller.forceFinished(true); // Begins the animation. scroller.fling( // Current scroll position. startX, startY, velocityX, velocityY, /* * Minimum and maximum scroll positions. The minimum scroll * position is generally 0 and the maximum scroll position * is generally the content size less the screen size. So if the * content width is 1000 pixels and the screen width is 200 * pixels, the maximum scroll offset is 800 pixels. */ 0, surfaceSize.x - contentRect.width(), 0, surfaceSize.y - contentRect.height(), // The edges of the content. This comes into play when using // the EdgeEffect class to draw "glow" overlays. contentRect.width() / 2, contentRect.height() / 2); // Invalidates to trigger computeScroll(). ViewCompat.postInvalidateOnAnimation(this); }
เมื่อ onFling() เรียก postInvalidateOnAnimation() จะทริกเกอร์ computeScroll() เพื่ออัปเดตค่าสำหรับ x และ y โดยทั่วไปแล้วการดำเนินการนี้จะเกิดขึ้นเมื่อมุมมองย่อยกำลังสร้างภาพเคลื่อนไหวการเลื่อนโดยใช้ออบเจ็กต์ตัวเลื่อน ดังที่แสดงในตัวอย่างก่อนหน้า
มุมมองส่วนใหญ่จะส่งตำแหน่ง x และ y ของออบเจ็กต์ตัวเลื่อนไปยัง scrollTo() โดยตรง
การติดตั้งใช้งาน computeScroll() ต่อไปนี้ใช้แนวทางที่แตกต่างออกไป โดยจะเรียก computeScrollOffset() เพื่อรับตำแหน่งปัจจุบันของ x และ y เมื่อตรงตามเกณฑ์สำหรับการแสดงเอฟเฟกต์ขอบ "เรืองแสง" ของการเลื่อนเกิน นั่นคือ จอแสดงผลซูมเข้า x หรือ y อยู่นอกขอบเขต และแอปยังไม่ได้แสดงการเลื่อนเกิน โค้ดจะตั้งค่าเอฟเฟกต์เรืองแสงของการเลื่อนเกินและเรียก postInvalidateOnAnimation() เพื่อทริกเกอร์การทำให้มุมมองไม่ถูกต้อง
Kotlin
// Edge effect/overscroll tracking objects. private lateinit var edgeEffectTop: EdgeEffect private lateinit var edgeEffectBottom: EdgeEffect private lateinit var edgeEffectLeft: EdgeEffect private lateinit var edgeEffectRight: EdgeEffect private var edgeEffectTopActive: Boolean = false private var edgeEffectBottomActive: Boolean = false private var edgeEffectLeftActive: Boolean = false private var edgeEffectRightActive: Boolean = false override fun computeScroll() { super.computeScroll() var needsInvalidate = false // The scroller isn't finished, meaning a fling or // programmatic pan operation is active. if (scroller.computeScrollOffset()) { val surfaceSize: Point = computeScrollSurfaceSize() val currX: Int = scroller.currX val currY: Int = scroller.currY val (canScrollX: Boolean, canScrollY: Boolean) = currentViewport.run { (left > AXIS_X_MIN || right < AXIS_X_MAX) to (top > AXIS_Y_MIN || bottom < AXIS_Y_MAX) } /* * If you are zoomed in, currX or currY is * outside of bounds, and you aren't already * showing overscroll, then render the overscroll * glow edge effect. */ if (canScrollX && currX < 0 && edgeEffectLeft.isFinished && !edgeEffectLeftActive) { edgeEffectLeft.onAbsorb(scroller.currVelocity.toInt()) edgeEffectLeftActive = true needsInvalidate = true } else if (canScrollX && currX > surfaceSize.x - contentRect.width() && edgeEffectRight.isFinished && !edgeEffectRightActive) { edgeEffectRight.onAbsorb(scroller.currVelocity.toInt()) edgeEffectRightActive = true needsInvalidate = true } if (canScrollY && currY < 0 && edgeEffectTop.isFinished && !edgeEffectTopActive) { edgeEffectTop.onAbsorb(scroller.currVelocity.toInt()) edgeEffectTopActive = true needsInvalidate = true } else if (canScrollY && currY > surfaceSize.y - contentRect.height() && edgeEffectBottom.isFinished && !edgeEffectBottomActive) { edgeEffectBottom.onAbsorb(scroller.currVelocity.toInt()) edgeEffectBottomActive = true needsInvalidate = true } ... } }
Java
// Edge effect/overscroll tracking objects. private EdgeEffectCompat edgeEffectTop; private EdgeEffectCompat edgeEffectBottom; private EdgeEffectCompat edgeEffectLeft; private EdgeEffectCompat edgeEffectRight; private boolean edgeEffectTopActive; private boolean edgeEffectBottomActive; private boolean edgeEffectLeftActive; private boolean edgeEffectRightActive; @Override public void computeScroll() { super.computeScroll(); boolean needsInvalidate = false; // The scroller isn't finished, meaning a fling or // programmatic pan operation is active. if (scroller.computeScrollOffset()) { Point surfaceSize = computeScrollSurfaceSize(); int currX = scroller.getCurrX(); int currY = scroller.getCurrY(); boolean canScrollX = (currentViewport.left > AXIS_X_MIN || currentViewport.right < AXIS_X_MAX); boolean canScrollY = (currentViewport.top > AXIS_Y_MIN || currentViewport.bottom < AXIS_Y_MAX); /* * If you are zoomed in, currX or currY is * outside of bounds, and you aren't already * showing overscroll, then render the overscroll * glow edge effect. */ if (canScrollX && currX < 0 && edgeEffectLeft.isFinished() && !edgeEffectLeftActive) { edgeEffectLeft.onAbsorb((int)mScroller.getCurrVelocity()); edgeEffectLeftActive = true; needsInvalidate = true; } else if (canScrollX && currX > (surfaceSize.x - contentRect.width()) && edgeEffectRight.isFinished() && !edgeEffectRightActive) { edgeEffectRight.onAbsorb((int)mScroller.getCurrVelocity()); edgeEffectRightActive = true; needsInvalidate = true; } if (canScrollY && currY < 0 && edgeEffectTop.isFinished() && !edgeEffectTopActive) { edgeEffectRight.onAbsorb((int)mScroller.getCurrVelocity()); edgeEffectTopActive = true; needsInvalidate = true; } else if (canScrollY && currY > (surfaceSize.y - contentRect.height()) && edgeEffectBottom.isFinished() && !edgeEffectBottomActive) { edgeEffectRight.onAbsorb((int)mScroller.getCurrVelocity()); edgeEffectBottomActive = true; needsInvalidate = true; } ... }
ส่วนของโค้ดที่ทำการซูมจริงมีดังนี้
Kotlin
lateinit var zoomer: Zoomer val zoomFocalPoint = PointF() ... // If a zoom is in progress—either programmatically // or through double touch—this performs the zoom. if (zoomer.computeZoom()) { val newWidth: Float = (1f - zoomer.currZoom) * scrollerStartViewport.width() val newHeight: Float = (1f - zoomer.currZoom) * scrollerStartViewport.height() val pointWithinViewportX: Float = (zoomFocalPoint.x - scrollerStartViewport.left) / scrollerStartViewport.width() val pointWithinViewportY: Float = (zoomFocalPoint.y - scrollerStartViewport.top) / scrollerStartViewport.height() currentViewport.set( zoomFocalPoint.x - newWidth * pointWithinViewportX, zoomFocalPoint.y - newHeight * pointWithinViewportY, zoomFocalPoint.x + newWidth * (1 - pointWithinViewportX), zoomFocalPoint.y + newHeight * (1 - pointWithinViewportY) ) constrainViewport() needsInvalidate = true } if (needsInvalidate) { ViewCompat.postInvalidateOnAnimation(this) }
Java
// Custom object that is functionally similar to Scroller. Zoomer zoomer; private PointF zoomFocalPoint = new PointF(); ... // If a zoom is in progress—either programmatically // or through double touch—this performs the zoom. if (zoomer.computeZoom()) { float newWidth = (1f - zoomer.getCurrZoom()) * scrollerStartViewport.width(); float newHeight = (1f - zoomer.getCurrZoom()) * scrollerStartViewport.height(); float pointWithinViewportX = (zoomFocalPoint.x - scrollerStartViewport.left) / scrollerStartViewport.width(); float pointWithinViewportY = (zoomFocalPoint.y - scrollerStartViewport.top) / scrollerStartViewport.height(); currentViewport.set( zoomFocalPoint.x - newWidth * pointWithinViewportX, zoomFocalPoint.y - newHeight * pointWithinViewportY, zoomFocalPoint.x + newWidth * (1 - pointWithinViewportX), zoomFocalPoint.y + newHeight * (1 - pointWithinViewportY)); constrainViewport(); needsInvalidate = true; } if (needsInvalidate) { ViewCompat.postInvalidateOnAnimation(this); }
นี่คือเมธอด computeScrollSurfaceSize() ที่เรียกใช้ในข้อมูลโค้ดก่อนหน้า ซึ่งจะคำนวณขนาดพื้นผิวที่เลื่อนได้ปัจจุบันเป็นพิกเซล ตัวอย่างเช่น หากพื้นที่แผนภูมิทั้งหมดมองเห็นได้ นี่คือขนาดปัจจุบันของ mContentRect หากซูมแผนภูมิเข้า 200% ในทั้ง 2 ทิศทาง ขนาดที่แสดงผลจะใหญ่เป็น 2 เท่าในแนวนอนและแนวตั้ง
Kotlin
private fun computeScrollSurfaceSize(): Point { return Point( (contentRect.width() * (AXIS_X_MAX - AXIS_X_MIN) / currentViewport.width()).toInt(), (contentRect.height() * (AXIS_Y_MAX - AXIS_Y_MIN) / currentViewport.height()).toInt() ) }
Java
private Point computeScrollSurfaceSize() { return new Point( (int) (contentRect.width() * (AXIS_X_MAX - AXIS_X_MIN) / currentViewport.width()), (int) (contentRect.height() * (AXIS_Y_MAX - AXIS_Y_MIN) / currentViewport.height())); }
ดูตัวอย่างการใช้งานตัวเลื่อนอีกตัวอย่างได้ที่
ซอร์สโค้ด
สำหรับคลาส ViewPager ซึ่งจะเลื่อนเพื่อตอบสนองต่อการสะบัดและใช้การเลื่อนเพื่อติดตั้งใช้งานภาพเคลื่อนไหว "snap-to-page"
ติดตั้งใช้งานเอฟเฟกต์การเลื่อนเลยขอบแบบยืด
ตั้งแต่ Android 12 เป็นต้นไป EdgeEffect จะเพิ่ม API ต่อไปนี้สำหรับการติดตั้งใช้งานเอฟเฟกต์การเลื่อนเกินขอบแบบยืด
getDistance()onPullDistance()
หากต้องการมอบประสบการณ์ของผู้ใช้ที่ดีที่สุดด้วยการเลื่อนเลยขอบแบบยืด ให้ทำดังนี้
- เมื่อภาพเคลื่อนไหวแบบยืดทำงานอยู่เมื่อผู้ใช้สัมผัสเนื้อหา ให้ลงทะเบียนการสัมผัสเป็นการ "จับ" ผู้ใช้จะหยุดภาพเคลื่อนไหวและ เริ่มจัดการการยืดอีกครั้ง
- เมื่อผู้ใช้เลื่อนนิ้วไปในทิศทางตรงข้ามกับการยืด, ให้ปล่อยการยืดจนกว่าจะหายไปทั้งหมด แล้วเริ่มเลื่อน
- เมื่อผู้ใช้สะบัดระหว่างการยืด ให้สะบัด
EdgeEffectเพื่อเพิ่มเอฟเฟกต์การยืด
จับภาพเคลื่อนไหว
เมื่อผู้ใช้จับภาพเคลื่อนไหวแบบยืดที่ทำงานอยู่ EdgeEffect.getDistance() จะแสดงผล 0 เงื่อนไขนี้บ่งชี้ว่าต้องจัดการการยืดด้วยการเคลื่อนไหวสัมผัส ในคอนเทนเนอร์ส่วนใหญ่ ระบบจะตรวจพบการจับใน onInterceptTouchEvent() ดังที่แสดงในข้อมูลโค้ดต่อไปนี้
Kotlin
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean { ... when (action and MotionEvent.ACTION_MASK) { MotionEvent.ACTION_DOWN -> ... isBeingDragged = EdgeEffectCompat.getDistance(edgeEffectBottom) > 0f || EdgeEffectCompat.getDistance(edgeEffectTop) > 0f ... } return isBeingDragged }
Java
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { ... switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: ... isBeingDragged = EdgeEffectCompat.getDistance(edgeEffectBottom) > 0 || EdgeEffectCompat.getDistance(edgeEffectTop) > 0; ... } }
ในตัวอย่างก่อนหน้า onInterceptTouchEvent() จะแสดงผล true เมื่อ mIsBeingDragged เป็น true ดังนั้นจึงเพียงพอที่จะใช้เหตุการณ์ก่อนที่องค์ประกอบย่อยจะมีโอกาสใช้เหตุการณ์
ปล่อยเอฟเฟกต์การเลื่อนเลยขอบ
คุณต้องปล่อยเอฟเฟกต์การยืดก่อนที่จะเลื่อนเพื่อป้องกันไม่ให้ใช้การยืดกับเนื้อหาที่เลื่อน ตัวอย่างโค้ดต่อไปนี้ใช้แนวทางปฏิบัติแนะนำนี้
Kotlin
override fun onTouchEvent(ev: MotionEvent): Boolean { val activePointerIndex = ev.actionIndex when (ev.getActionMasked()) { MotionEvent.ACTION_MOVE -> val x = ev.getX(activePointerIndex) val y = ev.getY(activePointerIndex) var deltaY = y - lastMotionY val pullDistance = deltaY / height val displacement = x / width if (deltaY < 0f && EdgeEffectCompat.getDistance(edgeEffectTop) > 0f) { deltaY -= height * EdgeEffectCompat.onPullDistance(edgeEffectTop, pullDistance, displacement); } if (deltaY > 0f && EdgeEffectCompat.getDistance(edgeEffectBottom) > 0f) { deltaY += height * EdgeEffectCompat.onPullDistance(edgeEffectBottom, -pullDistance, 1 - displacement); } ... }
Java
@Override public boolean onTouchEvent(MotionEvent ev) { final int actionMasked = ev.getActionMasked(); switch (actionMasked) { case MotionEvent.ACTION_MOVE: final float x = ev.getX(activePointerIndex); final float y = ev.getY(activePointerIndex); float deltaY = y - lastMotionY; float pullDistance = deltaY / getHeight(); float displacement = x / getWidth(); if (deltaY < 0 && EdgeEffectCompat.getDistance(edgeEffectTop) > 0) { deltaY -= getHeight() * EdgeEffectCompat.onPullDistance(edgeEffectTop, pullDistance, displacement); } if (deltaY > 0 && EdgeEffectCompat.getDistance(edgeEffectBottom) > 0) { deltaY += getHeight() * EdgeEffectCompat.onPullDistance(edgeEffectBottom, -pullDistance, 1 - displacement); } ...
เมื่อผู้ใช้ลาก ให้ใช้ระยะการดึง EdgeEffect ก่อนที่จะส่งการโต้ตอบแบบสัมผัสไปยังคอนเทนเนอร์การเลื่อนที่ซ้อนกันหรือลากการเลื่อน ในตัวอย่างโค้ดก่อนหน้า getDistance() จะแสดงผลค่าบวกเมื่อมีการแสดงเอฟเฟกต์ขอบและสามารถปล่อยได้ด้วยการเคลื่อนไหว เมื่อการโต้ตอบแบบสัมผัสปล่อยการยืด EdgeEffect จะใช้เหตุการณ์ดังกล่าวเป็นอันดับแรกเพื่อให้การยืดหายไปทั้งหมดก่อนที่จะแสดงเอฟเฟกต์อื่นๆ เช่น การเลื่อนที่ซ้อนกัน คุณสามารถใช้ getDistance() เพื่อเรียนรู้ว่าต้องใช้ระยะการดึงเท่าใดในการปล่อยเอฟเฟกต์ปัจจุบัน
onPullDistance() จะแสดงผลจำนวนเดลต้าที่ส่งผ่านซึ่งใช้ไปแล้ว ซึ่งแตกต่างจาก onPull() ตั้งแต่ Android 12 เป็นต้นไป หากมีการส่งค่า deltaDistance เป็นค่าลบไปยัง onPull() หรือ onPullDistance() เมื่อ getDistance() เป็น 0 เอฟเฟกต์การยืดจะไม่เปลี่ยนแปลง ใน Android 11 และเวอร์ชันก่อนหน้า onPull() จะอนุญาตให้ค่าลบสำหรับระยะทางทั้งหมดแสดงเอฟเฟกต์เรืองแสง
เลือกไม่ใช้การเลื่อนเกิน
คุณสามารถเลือกไม่ใช้การเลื่อนเกินในไฟล์เลย์เอาต์หรือโดยใช้โปรแกรม
หากต้องการเลือกไม่ใช้ในไฟล์เลย์เอาต์ ให้ตั้งค่า android:overScrollMode ดังที่แสดงในตัวอย่างต่อไปนี้
<MyCustomView android:overScrollMode="never"> ... </MyCustomView>
หากต้องการเลือกไม่ใช้โดยใช้โปรแกรม ให้ใช้โค้ดต่อไปนี้
Kotlin
customView.overScrollMode = View.OVER_SCROLL_NEVER
Java
customView.setOverScrollMode(View.OVER_SCROLL_NEVER);
แหล่งข้อมูลเพิ่มเติม
โปรดดูแหล่งข้อมูลที่เกี่ยวข้องต่อไปนี้