1

I am working on one project, I am using WordPress rest API to receive data in an android application, I have an activity where a user can scroll the messages in viewpager.

At a time on page number 1 only 10 messages are coming through rest API using /wp-json/posts/?page=1 (Because I have set 10 max posts in WordPress.) on /wp-json/posts/?page=2 next 10 messages are received and so on, How can implement it with viewpager using PagerAdapter? How to detect user has scrolled the last message and its time load next 10 messages dynamically? it's kinda lazy loading thing.

Here is my OnPageChangeListener

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            position_post = position;

            mVisibleItemCount = viewPager.getChildCount();
            mPastVisibleItems = viewPager.getOffscreenPageLimit();
            int totalItemCount = viewPager.getAdapter().getCount();

            if (mPostNum > mPreviousPostNum && !posts.isEmpty() && mVisibleItemCount != 0 &&
                    totalItemCount > mVisibleItemCount &&
                    (mVisibleItemCount + mPastVisibleItems) >= totalItemCount) {
                loadNextPage();

                mPreviousPostNum = mPostNum;
            }
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
CoderNik
  • 31
  • 5
  • Tried using `OnPageChangeListener` yet? http://stackoverflow.com/questions/11293300/determine-when-a-viewpager-changes-pages – OneCricketeer Mar 25 '17 at 14:58
  • @cricket_007 I am trying with that, but I am not able to understand how? – CoderNik Mar 25 '17 at 15:18
  • Your viewPager adapter has a `getCount()` method, right? And you have the `int position`, right? So, you should check when `position >= adapter.getCount()` and then lazy load from there . – OneCricketeer Mar 26 '17 at 00:07

0 Answers0