android - 当 Recyclerview 不在第一项时如何禁用 swiperefreshlayout?

标签 android material-design android-recyclerview swiperefreshlayout

我的 android 项目包括一个包含 cardView 列表的 recyclerView,并且在这个 recyclerView 的顶部还有一个 swipeRefreshLayout。当我向下滚动列表并拉出那些 cardView 时,我只想禁用 swipeRefreshLayout。换句话说,当 RecyclerView 不在第一个项目上时,如果用户想滚动回到第一个项目,它不能显示 swipeRefreshLayout。

我在谷歌上搜索了很多关于这个问题的信息,有一些解决这个问题的方法覆盖了 onScrollStateChanged 方法,但它们的表现不是很流畅,并且在某些情况下仍然启用 swipeRefreshLayout。

编辑 1: 以下链接包含我上面提到的其中一些解决方案:

https://stackoverflow.com/a/27042911/4257703

https://gist.github.com/NikolaDespotoski/1a6bb83dbae133f67812

这是我的 xml 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="8dp"
            android:choiceMode="none"
            android:focusable="false"
            android:listSelector="@android:color/darker_gray" />

        <ImageView
            android:id="@+id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center" />
    </FrameLayout>

</android.support.v4.widget.SwipeRefreshLayout>

编辑 2: 今天我意识到我的问题是因为一起实现 Tabs 和 swipeRefreshLayout 发生的。为了刷新包含 RecyclerView 的 Fragment 的数据,用户必须将页面拖到底部,另一方面,为了在选项卡之间切换,用户必须将屏幕向右或向左拖动。由于这种触摸手势,在滚动我的列表时会出现一些错误和滞后。请帮我解决这个问题。非常感谢。

最佳答案

也许我来晚了,但试试这个解决方案:

mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
mLayoutManager = new LinearLayoutManager(getActivity());    // a LinearLayoutManager
mRecyclerView.setLayoutManager(mLayoutManager);             // setting layoutManager on our RecyclerView

// Adding ScrollListener to getting whether we're on First Item position or not
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        mSwipeRefreshLayout.setEnabled(mLinearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0); // 0 is for first item position
    }
});

mSwipeRefreshLayout 是您的 SwipeRefreshLayout

输入上面的代码后,只有当您的第一个项目可见时,您才能滑动。

希望对您有所帮助! 😊

关于android - 当 Recyclerview 不在第一项时如何禁用 swiperefreshlayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33103428/

相关文章:

android - 如何让水平recyclerview项目出现在recyclerview的底部

android - 空指针异常 : While getting the bitmap from canvas

android - 如何在android中检测人体

android - 将android应用程序链接到另一个应用程序的最佳方法

java - 将 colorPrimary 转换为 colorPrimaryDark(暗多少)

jquery - 通过搜索选择 Material 设计

java - 等到任务完成

android - AppBarLayout fitSystemtWindows =“true"在 layout_height 为 wrap_content 时不起作用

android - 在 gradle Android Studio 中找不到方法 compile()

android - RecyclerView 和动画中的 notifyItemInserted()