OnNestedPreScroll returns opposite dy values ​​in a custom Behavior class

You have a problem. In my custom Behavior class, which I set for the custom view. I track the movement of the recycler view and get all offsets in the methods of the behavior class. onNestedScroll, onNestedPreScroll and then resize my view. And when my view expands and I try to collapse it, I get the opposite dy values ​​in the onNestedPreScroll method, for example: 40 -50 120 -130 300 -320, etc., But I move my finger in only one direction. Search all over the internet. Did not do anything. could you help me?

class CustomFilterBehavior : CoordinatorLayout.Behavior<CustomFilterViewTabLayout> {
private var skipNestedPreScroll = false

constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout,
                               child: CustomFilterViewTabLayout,
                               directTargetChild: View,
                               target: View?,
                               nestedScrollAxes: Int): Boolean {


 return nestedScrollAxes and ViewCompat.SCROLL_AXIS_VERTICAL != 0
}

override fun onNestedScroll(coordinatorLayout: CoordinatorLayout?,
                          child: CustomFilterViewTabLayout?,
                          target: View?,
                          dxConsumed: Int,
                          dyConsumed: Int,
                          dxUnconsumed: Int,
                          dyUnconsumed: Int) {
 if (dyUnconsumed < 0) {
   child?.moveLayout(dyUnconsumed, false)
   skipNestedPreScroll = true
 } else {
   skipNestedPreScroll = false
 }
}

override fun onNestedPreScroll(coordinatorLayout: CoordinatorLayout?, child: CustomFilterViewTabLayout?, target: View?, dx: Int, dy: Int, consumed: IntArray?) {
  if (dy != 0 && !skipNestedPreScroll) {
    child?.let {
      println(dy)
      consumed?.set(1, it.moveLayout(dy, true))
  }
 }
}

override fun onStopNestedScroll(coordinatorLayout: CoordinatorLayout?,
                              child: CustomFilterViewTabLayout?,
                              target: View?) {
  child?.stopScroll()
  skipNestedPreScroll = false
  super.onStopNestedScroll(coordinatorLayout, child, target)
 }
}
+4

Source: https://habr.com/ru/post/1672066/


All Articles