仅在 AppBarLayout 完全展开时启用 SwipeRefreshLayout

Enable SwipeRefreshLayout only when AppBarLayout expanded completely

如何仅在 AppBarLayout 完全展开时启用 SwipeRefreshLayout。我只需要在下一次滑动手势时启用刷新模式。现在,我试试

appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
  @Override
  public void onOffsetChanged(AppBarLayout appBarLayout, final int verticalOffset) {
    refreshLayout.setEnabled(verticalOffset == 0);
  }
});

当然可以!但它的工作方式并不像我需要的那样。当用户继续滑动手势时,此代码会立即启用刷新模式。我只需要在 AppBarLayout 扩展后的下一次滑动时启用它。
谁知道怎么做?

好吧,我遇到了同样的问题。这是我发现的。我的解决方案并不完美,但它对我有用。 假设我们有一个 AppBarLayout 和 RecyclerView 包装在 SwipeRefreshLayout 中。 注意力!检测到 Kotlin。

recyclerView.setOnTouchListener { _, motionEvent ->
        if (motionEvent.action == MotionEvent.ACTION_CANCEL
            || motionEvent.action == MotionEvent.ACTION_UP) {

            // 0.5f is used because I have snap parameter for CollapsingToolbar
            // and it automatically collapses/expands
            // if user finishes his scroll action in the middle of collapsing
            // so if AppBar is going to be completely expanded
            // we need to enable SwipeRefreshLayout
            swipeRefreshLayout.isEnabled =
                (appBarLayout.collapsingPercentage() < 0.5f)
        }
        false
    }

/**
 * @return 1.0f if AppBarLayout is completely collapsed,
 * 0.0f if completely expanded, percentage of
 * total height collapsed when collapsing/expanding is in progress.
 */
fun AppBarLayout.collapsingPercentage()
    = Math.abs(this.height - this.bottom) / this.totalScrollRange.toFloat()