Why addOnScrollListener that not work?

蓝咒 提交于 2019-12-11 08:44:39

问题


I try to implement load more when scroll to bottom of recyclerView It's work when my XML only have recyclerView but when i put it to scrollview and setNestedScrollingEnabled(false) it doesn't work

" Requirement " - The orange area is static layout - The green area is dynamic items and when i scoll to bottom orange area must be scroll down too

    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();
                    }

                }
            }
        }
    });

}

I try to debug 'dy' it's always 0


回答1:


dy is 0 because RecyclerView is not scrolling, its fitting its content in the scroll view. So, the view which is scrolling is ScrollView.

This is not a particularly good implementation as all the views in RecyclerView are inflated at the same time which beats the purpose of RecyclerView which is to reuse the ViewHolders when user scrolls and dynamically inflate elements in the View to save memory usage.

Try fixing the height of the RecyclerView and don't use wrap_content or match_parent in the height property of your RecyclerView



来源:https://stackoverflow.com/questions/44719703/why-addonscrolllistener-that-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!