androidandroid-recyclerviewandroid-viewpagerandroid-viewpager2endlessscroll

RecyclerView infinite scroll not working inside ViewPager2


I have a horizontal ViewPager2. On the second slide of the view pager, I have a RecyclerView that has infinite scrolling implemented. I used EndlessRecyclerViewScrollListener to implement infinite scroll.

However, when scrolling down this RecyclerView, it doesn't trigger infinite scrolling. Page 2 of the infinite scroll is never called, only page 1.

Here is my class with the ViewPager2:

public class BaseBottomNavigationViewActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    // The number of pages in the bottom navigation view
    private static final int NUM_PAGES = 5;

    // The view pager
    private ViewPager2 viewPager;

    // The pager adapter
    private FragmentStateAdapter pagerAdapter;

    // The bottom navigation view
    private BottomNavigationView bottomNavigationView;

    private NewPostBottomSheetDialogFragment newPostModalDialog;

    private RelativeLayout newPostButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        viewPager = findViewById(R.id.view_pager);
        bottomNavigationView = findViewById(R.id.bottom_navigation_view);

        // The number of screens to keep in memory
        viewPager.setOffscreenPageLimit(5);

        // Disable swiping in the view pager
        viewPager.setUserInputEnabled(false);

        // Set the pager adapter
        pagerAdapter = new BottomNavigationViewPagerAdapter(this);
        viewPager.setAdapter(pagerAdapter);

        // Register the item selected listener
        bottomNavigationView.setOnNavigationItemSelectedListener(this);

        // Set default selection (home)
        bottomNavigationView.setSelectedItemId(R.id.action_first);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_base_bottom_navigation_view;
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_first:
                viewPager.setCurrentItem(0, false);
                break;
            case R.id.action_second:
                viewPager.setCurrentItem(1, false);
                break;
            case R.id.action_third:
                return false;
            case R.id.action_fourth:
                viewPager.setCurrentItem(3, false);
                break;
            case R.id.action_fifth:
                viewPager.setCurrentItem(4, false);
                break;
            default:
                break;
        }

        return true;
    }

    private static class BottomNavigationViewPagerAdapter extends FragmentStateAdapter {
        public BottomNavigationViewPagerAdapter(FragmentActivity fa) {
            super(fa);
        }

        @Override
        public Fragment createFragment(int position) {
            switch (position) {
                case 0:
                    return new FirstPageFragment();
                case 1:
                    return new SecondPageFragment();
                case 2:
                    return new ThirdPageFragment();
                case 3:
                    return new FourthPageFragment();
                case 4:
                    return new FifthPageFragment();
            }

            return null;
        }

        @Override
        public int getItemCount() {
            return NUM_PAGES;
        }
    }

}

And here is the class for the second page in the ViewPager2:

public class SecondPageFragment extends Fragment {

    private MyAdapter myAdapter;

    private LinearLayoutManager layoutManager;

    private RecyclerView recyclerView;

    private EndlessRecyclerViewScrollListener scrollListener;

    private List<Object> objects = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_page_two, parent, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        recyclerView = view.findViewById(R.id.recycler_view);

        layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);

        myAdapter = new MyAdapter(getContext(), layoutManager, objects);
        recyclerView.setAdapter(myAdapter);

        scrollListener = new EndlessRecyclerViewScrollListener(layoutManager) {
            @Override
            public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
                getData(page);
            }
        };
        recyclerView.addOnScrollListener(scrollListener);
    }

    private void getData(final int page) {
        // Here I call the API to fetch the data and add them to the objects list
    }
}

Why is it not calling page 2 for the RecyclerView when scrolling in this situation and what can I do to fix it?

Thanks!


Solution

  • I ended up fixing this by just using a different infinite scroll RecyclerView class:

    https://stackoverflow.com/a/26561717/14968122