为什么addOnScrollListener不能正常工作? [英] Why addOnScrollListener that not work?

查看:1232
本文介绍了为什么addOnScrollListener不能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

滚动到recyclerView底部时,我尝试实现更多负载 当我的XML仅具有recyclerView时有效,但是当我将其放入scrollview和setNestedScrollingEnabled(false)时不起作用

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

                }
            }
        }
    });

}

我尝试调试"dy​​",它始终为0

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

推荐答案

dy为0,因为RecyclerView不滚动,因此其内容适合滚动视图.因此,正在滚动的视图为ScrollView.

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

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

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.

尝试固定RecyclerView的高度,并且不要在RecyclerView

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

这篇关于为什么addOnScrollListener不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆