Android SwipeRefreshLayout 、RecyclerView 下拉刷新冲突

来源:互联网 发布:3d算法必中直选 编辑:程序博客网 时间:2024/04/28 23:52

好久没写android了,发现5.0之后更新很大。在项目中SwipeRefreshLayout和RecyclerView一起使用,发现一些手机RecyclerView没有滑动到顶部,手指向下滑动时,触发了SwipeRefreshLayout的刷新事件,造成了冲突。

   根据多年经验,首先想到的是判断item目前是否在第一位置.代码如下:

    

rvFeed.setOnScrollListener(new RecyclerView.OnScrollListener() {            @Override            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {                FeedContextMenuManager.getInstance().onScrolled(recyclerView, dx, dy);                int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();                if(topRowVerticalPosition>0){                      swipeRefreshLayout.setRefreshing(false);                }else{                   // swipeRefreshLayout.setRefreshing(true);                    boolean isRefreshing = swipeRefreshLayout.isRefreshing();                    if (isRefreshing) {                        // adapter.notifyItemRemoved(adapter.getItemCount());                        return;                    }                    if (!isLoading) {                        isLoading = true;                        handler.postDelayed(new Runnable() {                            @Override                            public void run() {                                getData();                                Log.d("test", "load more completed");                                isLoading = false;                            }                        }, 1000);                    }                }            }        });
关键代码:

      

                int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();                if(topRowVerticalPosition>0){                      swipeRefreshLayout.setRefreshing(false);                }else{            }

0 0