当循环视图不在第一个项目上时,如何禁用swperefreshlayout? [英] How disable swiperefreshlayout when Recyclerview is not on the first item?

查看:36
本文介绍了当循环视图不在第一个项目上时,如何禁用swperefreshlayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android项目包括一个包含cardView列表的RececumerView,并且在此RececumerView的顶部还有一个swpeRefresh Layout。当我向下滚动列表并拉出那些cardView时,我只想禁用swpeRefresh Layout。换言之,当ReccyclerView不在第一个项目上时,如果用户想要回滚到第一个项目,则不能显示swpeRefresh Layout。

我在谷歌上搜索了很多关于此问题的信息,有一些解决方案会覆盖onScrollStateChanged方法,但它们的行为不是很流畅,在某些情况下仍会启用快速刷新布局。

编辑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和swpeRefresh Layout而出现的。要刷新包含循环视图的碎片的数据,用户必须将页面拖到底部,反之,为了在选项卡之间切换,用户必须向右或向左拖动屏幕。由于这种触摸手势,在滚动我的列表时会出现一些错误和滞后。请帮我解决这个问题。非常感谢。

推荐答案

可能我迟到了,但尝试一下此解决方案:

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

放置上述代码后,您只能在第一个项目可见时才能滑动。

希望这对您有帮助!😊

这篇关于当循环视图不在第一个项目上时,如何禁用swperefreshlayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆