android 仅在AppBarLayout完全展开时启用SwipeRefreshLayout

xqkwcwgp  于 2023-01-15  发布在  Android
关注(0)|答案(2)|浏览(140)

AppBarLayout完全展开时如何才能开启SwipeRefreshLayout?我需要在下一次滑动时开启刷新模式,现在我尝试一下

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

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

tez616oj

tez616oj1#

好吧,我也遇到了同样的问题。下面是我的发现。我的解决方案并不完美,但它对我很有效。让我们假设我们有一个 Package 在SwipeRefreshLayout中的AppBarLayout和RecyclerView。注意!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()
jjhzyzn0

jjhzyzn02#

应用程序栏布局?.addOnOffsetChangedListener(应用程序栏布局.OnOffsetChangedListener { _,垂直偏移-〉滑动布局?.已启用=垂直偏移== 0 })

相关问题