8

While implementing pagination. I faced this issue. Without scrolling the onscrolled method continuously calling. Please check my code below.

For reference, I'm using Endless Scrolling with AdapterViews and RecyclerView

There is also one demo example this.

I'm requesting data from API. So I implemented volley. After setup, strange thing happened. OnScrolled method continuously calling without scrolling. Below is my code.

    HomeFragment.java

    view = inflater.inflate(R.layout.fragment_home, container, false);

    tvNoDataFound = (TextView) view.findViewById(R.id.tv_no_data_found);
    recyclerView = (RecyclerView)view.findViewById(R.id.listView);

    linearLayoutManager = new LinearLayoutManager(getActivity());

    recyclerView.setNestedScrollingEnabled(false);

    recyclerView.setLayoutManager(linearLayoutManager);


    context = view.getContext();

    // Lookup the swipe container view
    swipeProjects = (SwipeRefreshLayout)view.findViewById(R.id.swipeProjects);

    // Setup refresh listener which triggers new data loading
    swipeProjects.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            // Your code to refresh the list here.
            // Make sure you call swipeContainer.setRefreshing(false)
            // once the network request has completed successfully.
            getProjects(0,0);
        }
    });
    // Configure the refreshing colors
    swipeProjects.setColorSchemeResources(android.R.color.holo_blue_bright,
            android.R.color.holo_green_light,
            android.R.color.holo_orange_light,
            android.R.color.holo_red_light);

    List<LatestProject> getLatest = getProjects(0,0);

    recyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener(linearLayoutManager) {

        @Override
        public void onLoadMore(int page, int totalItemsCount) {

            Log.e("here scrolling","scrolling---"+page+"---"+totalItemsCount);

            // Triggered only when new data needs to be appended to the list
            // Add whatever code is needed to append new items to the bottom of the list
            List<LatestProject> getLatest = getProjects(totalItemsCount,page);
        }
    });
return view;

after get the response from server, I used below function to set adapter.

public List<LatestProject> returnProjects(List<LatestProject> list, int offset, int curSizes){

    if(offset!=0){
        int curSize = adapter.getItemCount();
        latestProjectsList.addAll(list);
        adapter.notifyItemRangeInserted(curSize, list.size() - 1);
    }else{
        latestProjectsList = list;
        adapter = new ProjectListAdapter(list,getActivity(),HomeFragment.this);
        recyclerView.setAdapter(adapter);
    }
    return list;
}

In above code my first API call has offset 0. I don't know what is the issue. Is it because of volley asynchronous request?

2 Answers2

7

Yes it will be called. To overcome with this make one method

private boolean isLastItemDisplaying(RecyclerView recyclerView) {
    if (recyclerView.getAdapter().getItemCount() != 0) {
        int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
        if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
            return true;
    }
    return false;
}

now in onScolled method check with that method.

 boolean a = isLastItemDisplaying(recycler_product);
 if(a)
     {
        List<LatestProject> getLatest = getProjects(totalItemsCount,page);
     }
Piyush
  • 23,959
  • 6
  • 36
  • 71
  • 1
    Thanks for your help. But this logic is already present in EndlessRecyclerViewScrollListener.java. I only made one mistake. In the xml, recyclerView is inside the NestedScrollView. So once I removed the NestedScrollView, It's start working. So that's the problem. – Rockers Technology Sep 16 '16 at 05:01
  • 1
    can't we do this without removing NestedScrollView..? In my case i should keep NestedScrollView – GvSharma Nov 02 '17 at 12:46
  • 1
    recyclerview doesnt need scrollview, it itself has scroll view – GiridharaSPK Oct 09 '19 at 06:47
  • Still getting calling the last item twice. How can one avoid this multiple calls? – KSDev Jul 12 '20 at 13:02
  • thanks for your help. it is really working well! – user3069590 Apr 27 '21 at 11:12
0
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

                if (!isLoading) {
                    if (linearLayoutManager != null && linearLayoutManager.findLastCompletelyVisibleItemPosition() == rowsArrayList.size() - 1) {
                        //bottom of list!
                        loadMore();
                        isLoading = true;
                    }
                }
            }
        });

Source : JournalDev

GiridharaSPK
  • 551
  • 4
  • 14