SHARE
TWEET

Untitled

a guest May 23rd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.content.Context
  2. import android.util.AttributeSet
  3. import android.view.View
  4. import androidx.coordinatorlayout.widget.CoordinatorLayout
  5. import androidx.core.view.NestedScrollingChild3
  6. import androidx.core.view.NestedScrollingChildHelper
  7.  
  8. class NestedCoordinatorLayout @JvmOverloads constructor(
  9.     context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
  10. ) : CoordinatorLayout(context, attrs, defStyleAttr), NestedScrollingChild3 {
  11.  
  12.     private val nestedScrollingChildHelper: NestedScrollingChildHelper = NestedScrollingChildHelper(this)
  13.  
  14.     init {
  15.         isNestedScrollingEnabled = true
  16.     }
  17.  
  18.     override fun onStartNestedScroll(child: View, target: View, axes: Int, type: Int): Boolean =
  19.         super.onStartNestedScroll(child, target, axes, type).or(startNestedScroll(axes, type))
  20.  
  21.     override fun onNestedFling(target: View, velocityX: Float, velocityY: Float, consumed: Boolean): Boolean =
  22.         super.onNestedFling(target, velocityX, velocityY, consumed).or(
  23.             dispatchNestedFling(
  24.                 velocityX,
  25.                 velocityY,
  26.                 consumed
  27.             )
  28.         )
  29.  
  30.     override fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray, type: Int) {
  31.         super.onNestedPreScroll(target, dx, dy, consumed, type)
  32.  
  33.         if (consumed[1] == 0) {
  34.             dispatchNestedPreScroll(dx, dy, consumed, null, type)
  35.         }
  36.     }
  37.  
  38.     override fun onNestedScroll(
  39.         target: View,
  40.         dxConsumed: Int,
  41.         dyConsumed: Int,
  42.         dxUnconsumed: Int,
  43.         dyUnconsumed: Int,
  44.         type: Int,
  45.         consumed: IntArray
  46.     ) {
  47.         super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type, consumed)
  48.         if (consumed[1] == 0) {
  49.             dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, null, type, consumed)
  50.         }
  51.     }
  52.  
  53.     override fun onNestedPreFling(target: View, velocityX: Float, velocityY: Float): Boolean =
  54.         super.onNestedPreFling(target, velocityX, velocityY).or(dispatchNestedPreFling(velocityX, velocityY))
  55.  
  56.     override fun onStopNestedScroll(target: View, type: Int) {
  57.         super.onStopNestedScroll(target, type)
  58.         stopNestedScroll(type)
  59.     }
  60.  
  61.     override fun dispatchNestedScroll(
  62.         dxConsumed: Int,
  63.         dyConsumed: Int,
  64.         dxUnconsumed: Int,
  65.         dyUnconsumed: Int,
  66.         offsetInWindow: IntArray?,
  67.         type: Int
  68.     ): Boolean = nestedScrollingChildHelper.dispatchNestedScroll(
  69.         dxConsumed,
  70.         dyConsumed,
  71.         dxUnconsumed,
  72.         dyUnconsumed,
  73.         offsetInWindow,
  74.         type
  75.     )
  76.  
  77.     override fun dispatchNestedScroll(
  78.         dxConsumed: Int,
  79.         dyConsumed: Int,
  80.         dxUnconsumed: Int,
  81.         dyUnconsumed: Int,
  82.         offsetInWindow: IntArray?
  83.     ): Boolean = nestedScrollingChildHelper.dispatchNestedScroll(
  84.         dxConsumed,
  85.         dyConsumed,
  86.         dxUnconsumed,
  87.         dyUnconsumed,
  88.         offsetInWindow
  89.     )
  90.  
  91.     override fun dispatchNestedScroll(
  92.         dxConsumed: Int,
  93.         dyConsumed: Int,
  94.         dxUnconsumed: Int,
  95.         dyUnconsumed: Int,
  96.         offsetInWindow: IntArray?,
  97.         type: Int,
  98.         consumed: IntArray
  99.     ) {
  100.         nestedScrollingChildHelper.dispatchNestedScroll(
  101.             dxConsumed,
  102.             dyConsumed,
  103.             dxUnconsumed,
  104.             dyUnconsumed,
  105.             offsetInWindow,
  106.             type,
  107.             consumed
  108.         )
  109.     }
  110.  
  111.     override fun isNestedScrollingEnabled(): Boolean = nestedScrollingChildHelper.isNestedScrollingEnabled
  112.  
  113.     override fun dispatchNestedPreScroll(
  114.         dx: Int,
  115.         dy: Int,
  116.         consumed: IntArray?,
  117.         offsetInWindow: IntArray?,
  118.         type: Int
  119.     ): Boolean = nestedScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow, type)
  120.  
  121.     override fun dispatchNestedPreScroll(dx: Int, dy: Int, consumed: IntArray?, offsetInWindow: IntArray?): Boolean =
  122.         nestedScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow)
  123.  
  124.     override fun stopNestedScroll(type: Int) {
  125.         nestedScrollingChildHelper.stopNestedScroll(type)
  126.     }
  127.  
  128.     override fun stopNestedScroll() {
  129.         nestedScrollingChildHelper.stopNestedScroll()
  130.     }
  131.  
  132.     override fun hasNestedScrollingParent(type: Int): Boolean =
  133.         nestedScrollingChildHelper.hasNestedScrollingParent(type)
  134.  
  135.     override fun hasNestedScrollingParent(): Boolean = nestedScrollingChildHelper.hasNestedScrollingParent()
  136.  
  137.     override fun dispatchNestedPreFling(velocityX: Float, velocityY: Float): Boolean =
  138.         nestedScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY)
  139.  
  140.     override fun setNestedScrollingEnabled(enabled: Boolean) {
  141.         nestedScrollingChildHelper.isNestedScrollingEnabled = enabled
  142.     }
  143.  
  144.     override fun dispatchNestedFling(velocityX: Float, velocityY: Float, consumed: Boolean): Boolean =
  145.         nestedScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed)
  146.  
  147.     override fun startNestedScroll(axes: Int, type: Int): Boolean =
  148.         nestedScrollingChildHelper.startNestedScroll(axes, type)
  149.  
  150.     override fun startNestedScroll(axes: Int): Boolean = nestedScrollingChildHelper.startNestedScroll(axes)
  151. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top