为什么addOnScrollListener不起作用?

时间:2017-06-23 10:55:19

标签: android android-recyclerview

滚动到recyclerView的底部时,我尝试实现更多的加载 当我的XML只有recyclerView但是当我把它放到scrollview和setNestedScrollingEnabled(false)时它不起作用

XML “要求” - 橙色区域是静态布局 - 绿色区域是动态项目 当我到底部橙色区域时,也必须向下滚动

    mAdapter = new RecyclerViewCommentAdapter(commentList, userInformationList);
    mRecyclerViewComment = (RecyclerView) rootView.findViewById(R.id.recyclerViewComment);
    mRecyclerViewComment.setNestedScrollingEnabled(false);
    mRecyclerViewComment.setHasFixedSize(true);
    mRecyclerViewComment.setItemViewCacheSize(30);
    mRecyclerViewComment.setDrawingCacheEnabled(true);
    mRecyclerViewComment.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

    mLayoutManager = new LinearLayoutManager(mContext);
    mRecyclerViewComment.setLayoutManager(mLayoutManager);
    mRecyclerViewComment.setItemAnimator(new DefaultItemAnimator());
    mRecyclerViewComment.setAdapter(mAdapter);

    // Scroll //
    mRecyclerViewComment.addOnScrollListener(new RecyclerView.OnScrollListener()
    {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy)
        {
            Log.d(getClass().getName(), "dy = " + dy);
            if(dy > 0) //check for scroll down
            {
                visibleItemCount = mLayoutManager.getChildCount();
                totalItemCount = mLayoutManager.getItemCount();
                pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
                Log.d(getClass().getName(), "totalItemCount = " + totalItemCount);
                if (loading)
                {

                    if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount && (visibleItemCount + pastVisiblesItems) >= TOTAL_FIRST_LOAD)
                    {
                        loading = false;
                        loadMoreKey();
                    }

                }
            }
        }
    });

}

我尝试调试'dy'它始终为0

1 个答案:

答案 0 :(得分:1)

dy为0,因为RecyclerView不滚动,它在滚动视图中拟合其内容。因此,滚动的视图是ScrollView

这不是一个特别好的实现,因为RecyclerView中的所有视图都在同时被夸大,这超出了RecyclerView的目的,即在用户滚动时重用ViewHolders动态膨胀视图中的元素以节省内存使用量。

尝试修复RecyclerView的高度,不要在wrap_content

match_parent属性中使用heightRecyclerView