当 Recyclerview 不在第一项时如何禁用 swiperrefreshlayout?

moh*_*mad 2 android swiperefreshlayout material-design android-recyclerview

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

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

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

/sf/answers/1893003801/

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>
Run Code Online (Sandbox Code Playgroud)

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

Mee*_*eet 10

也许我迟到了,但试试这个解决方案:

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
    }
});
Run Code Online (Sandbox Code Playgroud)

mSwipeRefreshLayout 是你的 SwipeRefreshLayout

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

希望这可以帮助!