如何在Scroll StaggeredGrid RecyclerView上控制SwipeRefreshLayout和Endless Fuction?

时间:2015-07-27 03:38:37

标签: android android-recyclerview swiperefreshlayout endlessscroll staggeredgridlayout

我尝试使用Endless和SwipeRefreshLayout组合StaggeredGrid RecyclerView。但是我的代码有问题:

grid.addOnScrollListener(new RecyclerView.OnScrollListener() {
           @Override
           public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
               super.onScrolled(recyclerView, dx, dy);
               int visibleItemCount = grid.getChildCount();
               int totalItemCount = mLayoutManager.getItemCount();
               int firstVisibleItem = mLayoutManager.findFirstVisibleItemPositions(null)[0];
               if (firstVisibleItem == 0 && visibleItemCount > 0
                       && grid.getChildAt(0).getTop() >= 0) {
                   mSwipeRefreshLayout.setEnabled(true);
               } else {
                   mSwipeRefreshLayout.setEnabled(false);
               }
               mPreviousVisibleItem = firstVisibleItem;
               if (firstVisibleItem + visibleItemCount == totalItemCount
                       && totalItemCount != 0
                       && isFinishLoadingAwal
                       && !isFinishMoreNews
                       && adapter.getItemCount() > 0) {
                   getMoreNewsFromServer();
               }
           }
       });

上面的代码,我遇到了问题:

  1. 在无休止的位置,getMoreNewsFromServer();总是被叫(正常:一个叫)。

  2. 在无休止的位置,mSwipeRefreshLayout被称为

  3. 如果我使用Listview,它的工作方式如下:

    list.setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                }
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    if (firstVisibleItem == 0 && visibleItemCount > 0
                            && list.getChildAt(0).getTop() >= 0) {
                        mSwipeRefreshLayout.setEnabled(true);
                    } else {
                        mSwipeRefreshLayout.setEnabled(false);
                    }
                    mPreviousVisibleItem = firstVisibleItem;
                    if (firstVisibleItem + visibleItemCount == totalItemCount
                            && totalItemCount != 0
                            && isFinishLoadingAwal
                            && !isFinishMoreNews
                            && adapter.getCount() > 0) {
                        getMoreNewsFromServer();
                    }
                }
            });
    

    那么如何解决呢?

1 个答案:

答案 0 :(得分:-1)

第一件事:

  

“伸出眼睛无法触及的地方”

onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)

所以在代码片段中用 view 替换 list - >

   && list.getChildAt(0).getTop() >= 0 

<强>第二

使用mSwipeRefreshLayout是有风险的,不用检查它是否为空! 你可以尝试在onScroll(){

开始时进行inser
if (mSwipeRefreshLayout == null) {
   mSwipeRefreshLayout = (SwipeRefreshLayout) view.getRootView().findViewById(R.id...);
} else { .. }

第三:

你不体验refresh bug吗?但也许你的应用的用户呢?