简体   繁体   English

RecyclerView OnScrollListener()问题

[英]RecyclerView OnScrollListener() Issue

I have around 32 records in json, I am using RecyclerView to show them and I have implemented OnScrollListener(...) 我在json中有大约32条记录,我使用RecyclerView来显示它们并且我已经实现了OnScrollListener(...)

Question

I started an Activity, I fetched all 32 records, now when I do scroll, why I am again getting same 32 records again and again, whenever I do scroll, here is my implementation of OnScrollListener() 我开始一个Activity,我获取了所有32条记录,现在当我滚动时,为什么我一次又一次地获得相同的32条记录,每当我滚动时,这里是我的OnScrollListener()的实现

public void initializeOnScrollForRecyclerView() {
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                int visibleItemCount = recyclerView.getLayoutManager().getChildCount();
                int totalItemCount = recyclerView.getLayoutManager().getItemCount();
                int pastVisiblesItems = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();

                if (!isLoading) {
                    if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                        isLoading = true;
                        mPostPresenter.loadPosts(false);
                    }
                }
            }
        });
    }

The Implementation seems to be correct but for one condition it fails, try when dy > 0 like this (Also put this in the OnCreate of the Activity) : 实现似乎是正确的但是对于一个条件它失败了,尝试dy> 0时这样(也把它放在Activity的OnCreate中):

    private boolean loading = true;

 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }


        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy)
        {

            if (loading) {
                if (dy > 0) //check for scroll down
                {
                    visibleItemCount = layoutManager.getChildCount();
                    totalItemCount = layoutManager.getItemCount();
                    pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();

                    if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                        loading = false;

                        Log.v("...", " Reached Last Item");
                        loadMoreVideos(searchVideos);
                    }

                }
            }
        }
    });

You are using this 你正在使用它

 if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) 

Which means once you have viewed 22 records you are loading again. 这意味着一旦您查看了22条记录,您就会再次加载。 And while loading again you are getting same data and appending it to your list. 再次加载时,您将获得相同的数据并将其附加到列表中。

I can see that you are using RXjava. 我可以看到你正在使用RXjava。 When you are subscribing in loadPost check what data your observable is emitting. 当您在loadPost中订阅时,请检查您的observable正在发出什么数据。 i think its emitting the same data again ie your 32 records and those records are added again and this is an endless loop. 我认为它再次发出相同的数据,即你的32条记录和那些记录再次添加,这是一个无限循环。

Here's another way to trigger something when last item is reached 这是在达到最后一个项目时触发某事的另一种方法

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

    //When last item is reached
    if(position == yourList.size() - 1){
        loadMoreMessages();
    }
  }

public abstract void loadMoreMessages();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM