JustKhit
JustKhit

Reputation: 407

Why addOnScrollListener that not work?

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

XML " 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

Upvotes: 2

Views: 2694

Answers (1)

Abhishek Tyagi
Abhishek Tyagi

Reputation: 142

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

Upvotes: 1

Related Questions