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) {
}
});