1. 程式人生 > >完美解決SwipeRefreshLayout與RecyclerView滑動衝突問題

完美解決SwipeRefreshLayout與RecyclerView滑動衝突問題

直接上程式碼

        //解決swipelayout與Recyclerview的衝突
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                int topRowVerticalPosition =
                        (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
                swipeRefreshLayout.setEnabled(topRowVerticalPosition >= 0);
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
        });
另外網上還有重寫Recyclerview的方法,親測不管用,直接監聽Recyclerview的滑動即可。