CymChad / BaseRecyclerViewAdapterHelper

BRVAH:Powerful and flexible RecyclerAdapter
http://www.recyclerview.org/
MIT License
24.33k stars 5.16k forks source link

列表快速上滑onLoadMoreRequested调用2次 #1790

Closed zofnk closed 6 years ago

zofnk commented 6 years ago

遇到个很奇怪的问题,不知道从何改起,我这有个列表,上面有2个headview,当快速往上滑动的时候就会触发2次onLoadMoreRequested , 缓慢滑动的话就是正常的触发一次...

初始化适配器 private void initAdapter() { GridLayoutManager scheduleLm = new GridLayoutManager(getContext(), 2); LinearLayoutManager newsLm = new LinearLayoutManager(getContext()); ((SimpleItemAnimator) mRvHome.getItemAnimator()).setSupportsChangeAnimations(false); mSrlHome.setColorSchemeColors(getResources().getColor(R.color.colorPrimary)); mNewsAdapter = new NewsAdapter(null, true); mScheduleAdapter = new HomeScheduleAdapter(null); View imagesBannerView = mInflater.inflate(R.layout.layout_banner_home, null); mBannerView = (HomeImageBanner) imagesBannerView.findViewById(R.id.bannerHome);

    View homeScheduleView = mInflater.inflate(R.layout.layout_home_schedule, null);
    RecyclerView rvSchedule = (RecyclerView) homeScheduleView.findViewById(R.id.rvHomeSchedule);

    rvSchedule.setLayoutManager(scheduleLm);
    rvSchedule.setAdapter(mScheduleAdapter);

    //如果mark不为推荐模块就不添加BannerView的HeadView
    if (TextUtils.equals(DataServer.homeLeagueTeamList().get(0).getMark(), mark)) {
        mNewsAdapter.addHeaderView(imagesBannerView, 0);
        mNewsAdapter.addHeaderView(homeScheduleView, 1);
    } else {
        mNewsAdapter.addHeaderView(homeScheduleView);
    }
    mRvHome.setLayoutManager(newsLm);
    mRvHome.setAdapter(mNewsAdapter);
    mSrlHome.setOnRefreshListener(this);
    mNewsAdapter.setOnLoadMoreListener(this, mRvHome);
}

处理数据 private void getHomeIndexDatas(boolean isRefresh) { DataServer.homeIndex(mark, page, new LoadingViewCallBack(getContext()) { @Override public void onSuccess(Response response) { mHomeEntity = new Gson().fromJson(response.body(), HomeEntity.class); if (mHomeEntity.isShowAlert()) { if (mHomeEntity.isOk()) { ToastUtils.successToast(getContext(), mHomeEntity.getMsg()); } } if (mHomeEntity.isOk()) { List newsList = mHomeEntity.getData().getNewsList(); List scheduleList = mHomeEntity.getData().getScheduleList(); mBannerList = mHomeEntity.getData().getSlideshowList();

                if (mBannerList.size() != 0 && isRefresh) {
                    mBannerView.setSource(mBannerList);
                    mBannerView.startScroll();
                }

                for (NewsListEntity entity : newsList) {
                    if (NewsDaoHelper.queryNew(entity) == null) {
                        NewsDaoHelper.insertNews(entity);
                    } else {
                        entity.setIsRead(NewsDaoHelper.queryNew(entity).getIsRead());
                    }
                }

                if (isRefresh) {
                    mNewsAdapter.setNewData(newsList);
                    mScheduleAdapter.setNewData(scheduleList);
                } else {
                    mNewsAdapter.addData(newsList);
                    mNewsAdapter.loadMoreComplete();
                }
            } else {
                mNewsAdapter.loadMoreFail();
            }
        }
    });
}

刷新回调 @Override public void onRefresh() { page = 1; getHomeIndexDatas(true); mSrlHome.setRefreshing(false); mNewsAdapter.setEnableLoadMore(true); }

上拉加载回调 @Override public void onLoadMoreRequested() { mSrlHome.setRefreshing(false); mSrlHome.setEnabled(false); if (mHomeEntity.getData().isHasNext()) { page++; getHomeIndexDatas(false); } else { mNewsAdapter.loadMoreEnd(); } mSrlHome.setEnabled(true); }

juziml commented 6 years ago

你上面的代码中有业务代码耦合判断。我建议你单独写个测试。不要有业务代码,加载模拟数据打印onLoadMoreRequested方法。如果还有问题再贴测试代码,这样会很好的帮助你发现问题

1109993488 commented 6 years ago

你试试DataServer延迟几秒返回数据看是否回调onLoadMoreRequested2次

zofnk commented 6 years ago

原因找到了 我在adapter构造方法里不知道怎么多加了一句setHasStableIds(true), 麻烦求教下为什么加了这句之后快速上拉会触发2次上拉回调 ..?

1109993488 commented 6 years ago

跟这句应该没影响吧

zofnk commented 6 years ago

我去掉之后就没事了 , 我之后看看能不能复现一个demo试看看 , 到时再反应一下~谢谢了