android - 当 RecyclerView 不在第一项时,如何禁用滑动刷新布局?

标签 android android-recyclerview swiperefreshlayout

我在我的项目中使用了 swiperefreshlayout 和 recyclerview。当 recyclerview 不在第一项时,我希望禁用 swiperefreshlayout。我该怎么做? 我有一些代码,但没有用:

int topRowVerticalPosition = (myRecyclerView == null || myRecyclerView.getChildCount() == 0) ? 0 : myRecyclerView.getChildAt(0).getTop();
                swipeRefreshLayout.setEnabled(layoutManager.findFirstCompletelyVisibleItemPosition() == 0 && topRowVerticalPosition >= 0);

最佳答案

您需要像这样向 RecyclerView 添加 OnScrollListener,并且您可以使用 RecyclerView 的第一个子项的可见性启用/禁用 swipeRefreshLayout:

myRecyclerView.addOnScrollListener(new OnScrollListener() {
  @Override
  public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
    View firstChild = recyclerView.getChildAt(0);
    swipeRefreshLayout.setEnabled(firstChild.getVisibility() == View.VISIBLE);
  }
});

关于android - 当 RecyclerView 不在第一项时,如何禁用滑动刷新布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55575732/

相关文章:

Android 预加载 Recycleview 内容

android - SwipeToRefresh 不遵守 Android 中的 requestDisallowInterceptTouchEvent()

android - 如何与 OBD II ELM327 适配器​​进行连续通信?

android - AutocompleteTextView 建议列表上升

android - 如何查找应用程序随时间的电池使用情况?

android - RecyclerView Laggy inside NestedScrollView/LinearLayout

java - 如何将对象树传递到包中?

java - 避免按后退按钮退出应用程序

android - androidx 迁移后,react-native 应用程序上出现 SwipeRefreshLayout 错误

android - 刷新时禁用默认的 SwipeRefreshLayout 动画