10

我的布局有问题,我在里面创建了带有 RecyclerView 的 SwipeRefreshLayout。在 android 4.2.2+ 中一切正常,但在 andorid 2.3.4 中我无法向上滚动,因为在 RecyclerView 的任何地方它都会刷新,我必须向下滚动然后向上滚动。

这是我的代码:

<android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/forum_swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LVP"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center" />
</android.support.v4.widget.SwipeRefreshLayout>

我发现了这个问题:https ://code.google.com/p/android/issues/detail?id=78191但没有解决方案。知道如何解决吗?

4

6 回答 6

10

覆盖 RecyclerView 的方法OnScrollStateChanged

 mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                // TODO Auto-generated method stub
                //super.onScrollStateChanged(recyclerView, newState);
               try {
                   int firstPos = mLayoutManager.findFirstCompletelyVisibleItemPosition();
                   if (firstPos > 0) {
                       mSwipeRefreshLayout.setEnabled(false);
                   } else {
                       mSwipeRefreshLayout.setEnabled(true);
                       if(mRecyclerView.getScrollState() == 1)
                           if(mSwipeRefreshLayout.isRefreshing())
                            mRecyclerView.stopScroll();
                   }

               }catch(Exception e) {
                   Log.e(TAG, "Scroll Error : "+e.getLocalizedMessage());
               }
            }

检查刷卡刷新是否正在刷新并尝试向上滚动然后出现错误,所以当刷卡刷新正在进行时,我尝试这样做mRecyclerView.stopScroll();

于 2015-07-30T04:33:09.630 回答
6

我使用以下代码修复了向上滚动问题:

private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        LinearLayoutManager manager = ((LinearLayoutManager)recyclerView.getLayoutManager());
        boolean enabled =manager.findFirstCompletelyVisibleItemPosition() == 0;
        pullToRefreshLayout.setEnabled(enabled);
    }
};

然后,您需要使用setOnScrollListeneraddOnScrollListener取决于您是否有一个或多个听众。

于 2015-12-15T11:45:27.850 回答
5

不幸的是,这是一个已知问题,将在未来的版本中修复。

https://code.google.com/p/android/issues/detail?id=78191

同时,如果您需要紧急修复,请覆盖canChildScrollUpSwipeRefreshLayout.java致电recyclerView.canScrollVertically(mTarget, -1)。因为canScrollVertically是在姜饼之后添加的,所以您还需要复制该方法并在 recyclerview 中实现。

或者,如果您使用的是 LinearLayoutManager,您可以调用findFirstCompletelyVisibleItemPosition.

带来不便敬请谅解。

于 2015-01-04T09:27:50.393 回答
3

您可以根据 recyclerview 的滚动功能禁用/启用刷新布局

    public class RecyclerSwipeRefreshHelper extends RecyclerView.OnScrollListener{

    private static final int DIRECTION_UP = -1;


    private final SwipeRefreshLayout refreshLayout;

    public RecyclerSwipeRefreshHelper(
            SwipeRefreshLayout refreshLayout) {
        this.refreshLayout = refreshLayout;
    }


    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
       refreshLayout.setEnabled((recyclerView.canScrollVertically(DIRECTION_UP)));
    }
}
于 2015-08-31T19:20:59.537 回答
1

您可以像这样覆盖 SwipeRefreshLayout 中的 canChildScrollUp() 方法:

public boolean canChildScrollUp() {
    if (mTarget instanceof RecyclerView) {
        final RecyclerView recyclerView = (RecyclerView) mTarget;
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof LinearLayoutManager) {
            int position = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
            return position != 0;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int[] positions = ((StaggeredGridLayoutManager) layoutManager).findFirstCompletelyVisibleItemPositions(null);
            for (int i = 0; i < positions.length; i++) {
                if (positions[i] == 0) {
                    return false;
                }
            }
        }
        return true;
    } else if (android.os.Build.VERSION.SDK_INT < 14) {
        if (mTarget instanceof AbsListView) {
            final AbsListView absListView = (AbsListView) mTarget;
            return absListView.getChildCount() > 0
                    && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
                            .getTop() < absListView.getPaddingTop());
        } else {
            return mTarget.getScrollY() > 0;
        }
    } else {
        return ViewCompat.canScrollVertically(mTarget, -1);
    }
}
于 2015-01-04T04:00:07.537 回答
0

Based on @wrecker answer (https://stackoverflow.com/a/32318447/7508302).

In Kotlin we can use extension method. So:

class RecyclerViewSwipeToRefresh(private val refreshLayout: SwipeToRefreshLayout) : RecyclerView.OnScrollListener() {
    companion object {
        private const val DIRECTION_UP = -1
    }

    override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
        super.onScrolled(recyclerView, dx, dy)
        refreshLayout.isEnabled = !(recyclerView?.canScrollVertically(DIRECTION_UP) ?: return)
    }
}

And let's add extension method to RecyclerView to easly apply this fix to RV.

fun RecyclerView.fixSwipeToRefresh(refreshLayout: SwipeRefreshLayout): RecyclerViewSwipeToRefresh {
    return RecyclerViewSwipeToRefresh(refreshLayout).also {
        this.addOnScrollListener(it)
    }
}

Now, we can fix recyclerView using:

recycler_view.apply {
     ...
     fixSwipeToRefresh(swipe_container)
     ...
}
于 2018-11-19T10:38:13.557 回答