-1

I using GridLayoutManager with 3 span count in recyclerView on my app, the problem is when I run the app first time it's showing only first 10 items due to the the restriction / pagiation of the items from server, when user refreshing with SwipeRefreshLayout or scrolling it's continue loading other items, but it's look ugly on first load of items so I want fill these gaps without necessary for refreshing to load other items

here's the gridlayout with scroll listener

gridLayoutManager = new GridLayoutManager(MainActivity.this, 3);

 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                    isScrolling = true;
                    progressBar.setVisibility(View.GONE);

                }

            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0) {
                        currentItems = gridLayoutManager.getChildCount();
                        totalItems = gridLayoutManager.getItemCount();
                        scrollOutItems = gridLayoutManager.findFirstVisibleItemPosition();

                    if (isScrolling && (currentItems + scrollOutItems == totalItems)) {
                        isScrolling = false;
                        if (getItemsByLabelCalled) {
                            for (int i = 1; i < 7; i++) {
                                if (navigationView.getMenu().getItem(i).isChecked()) {
                                    getItemsByLabel(navigationView.getMenu().getItem(i).getTitle().toString());
                                }
                            }
                        } else {
                            getData();
                        }
                    }
                }
            }
        });

SwipeRefreshLayout

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (navigationView.getMenu().getItem(0).isChecked()) {
                    if (Utils.hasNetworkAccess(MainActivity.this)) {
                        getData();
                    } else {
                        Toast.makeText
                                (MainActivity.this, R.string.SwipeRefreshLayout_connect_to_update
                                        , Toast.LENGTH_LONG).show();
                    }
                } else {
                    for (int i = 1; i < 7; i++) {
                        if (navigationView.getMenu().getItem(i).isChecked()) {
                            getItemsByLabel(navigationView.getMenu().getItem(i).getTitle().toString());
                        }
                    }
                }

                new Handler().postDelayed(() -> swipeRefreshLayout.setRefreshing(false), 2000);
            }

        });

The problem described with images

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
  • I am a beginner... But sometimes this helps. [RecyclerView pagination Example](https://androidride.com/android-recyclerview-load-more-on-scroll-example/) – Vijay Ram Mar 31 '20 at 08:36

1 Answers1

1

The problem is that your items count is not enough to fill the entire page. There are two ways to overcome this issue.

  1. You can call your getData method one more time after loading the first batch to get more data which is not a clean solution since you might need more items on larger screens like tablets.

  2. Put your load more procedure in onBindViewHolder method of your recycler view adapter instead of its on scroll listener. You can do the load more if position == getItemCount()-1 for instance which requests server for more items if your last item gets in view (or needs binding). Remember that you have to hold a boolean in order to prevent repetitive calls on your getData.

Sdghasemi
  • 5,370
  • 1
  • 34
  • 42
  • Thanks for answering, but the items is more than ten of course, and I tested it with linear layout manager, see the GIF when I refresh its load the other items, the second solution seems good but I cannot call `getData` from my adapter class although it is public – Dr Mido Mar 25 '19 at 15:24
  • Of course you have more than 10 items, but each of your batches contains 10. You can create an interface with your desired methods like `getData` and implement it in your activity/fragment (where you declared your method already) and pass and hold a reference to it in your adapter and call the methods whenever you want. – Sdghasemi Mar 25 '19 at 15:30