Last active
November 8, 2019 06:50
-
-
Save hosseiniSeyRo/aa38b9357c8499867eb0875f54f4a422 to your computer and use it in GitHub Desktop.
pagination in recyclerView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private boolean isLoading = false; | |
private boolean isEndOfList = false; | |
private String currentPage = "1"; | |
. | |
. | |
. | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
. | |
. | |
. | |
/* Init user video recyclerView */ | |
initUserVideosRecyclerView(); | |
/* Get list from server */ | |
currentPage = "1"; | |
getListFromServer(currentPage); | |
/* Handle pagination */ | |
rv_userVideos.addOnScrollListener(new RecyclerView.OnScrollListener() { | |
@Override | |
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { | |
super.onScrollStateChanged(recyclerView, newState); | |
if (!recyclerView.canScrollVertically(1) && !isEndOfList && !isLoading) { | |
currentPage = String.valueOf(Integer.valueOf(currentPage) + 1); | |
getListFromServer(currentPage); | |
} | |
} | |
}); | |
. | |
. | |
. | |
} | |
/* Init user video recyclerView */ | |
private void initUserVideosRecyclerView() { | |
// set recyclerView orientation | |
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2); | |
rv_userVideos.setLayoutManager(mLayoutManager); | |
// set recyclerView item decoration | |
// rv_userVideos.addItemDecoration(new GridSpacingItemDecoration(1, dpToPx(5), true)); | |
rv_userVideos.setItemAnimator(new DefaultItemAnimator()); | |
// set recyclerView Adapter | |
userVideoRecyclerAdapter = new UserVideoRecyclerAdapter(this, userVideoList, true); | |
rv_userVideos.setAdapter(userVideoRecyclerAdapter); | |
userVideoRecyclerAdapter.setOnItemClickListener(position -> { | |
//TODO | |
}); | |
} | |
/* Get list from server */ | |
private void getListFromServer(String pageNumber) { | |
// definition api interface | |
APIInterface apiInterface = RetrofitClient.getRetrofitInstance().create(APIInterface.class); | |
// calling getAdultUserVideos method in api interface | |
Call<UserVideoListResponse> call = apiInterface.getAdultUserVideos(pageNumber); | |
// handle progress visibility | |
if (pageNumber.equals("1")) { | |
// show progress and hide content before call | |
pageContentLoadingContainer.setVisibility(View.VISIBLE); | |
pageContentContainer.setVisibility(View.GONE); | |
} else { | |
//show rv progress | |
recyclerViewProgress.setVisibility(View.VISIBLE); | |
} | |
// call request | |
call.enqueue(new Callback<UserVideoListResponse>() { | |
@Override | |
public void onResponse(Call<UserVideoListResponse> call, Response<UserVideoListResponse> response) { | |
isLoading = false | |
// handle progress visibility | |
if (pageNumber.equals("1")) { | |
// hide progress and show content | |
pageContentLoadingContainer.setVisibility(View.GONE); | |
pageContentContainer.setVisibility(View.VISIBLE); | |
} else { | |
recyclerViewProgress.setVisibility(View.GONE); | |
} | |
// get response | |
if (response.isSuccessful() && response.body() != null) { | |
// handle empty recyclerView | |
if (pageNumber.equals("1") && response.body().getUserVideoListWithPagination().getUserVideoList().size() == 0) { | |
emptyRecyclerViewText.setVisibility(View.VISIBLE); | |
} else { | |
emptyRecyclerViewText.setVisibility(View.GONE); | |
} | |
// add data to list | |
if (response.body().getUserVideoListWithPagination().getUserVideoList().size() != 0) { | |
userVideoList.addAll(response.body().getUserVideoListWithPagination().getUserVideoList()); | |
userVideoRecyclerAdapter.notifyDataSetChanged(); | |
} | |
nextUrl = response.body().getUserVideoListWithPagination().getNext_page_url(); | |
isEndOfList = nextUrl == null; | |
} else { | |
// Log | |
} | |
} | |
@Override | |
public void onFailure(Call<UserVideoListResponse> call, Throwable t) { | |
isLoading = false | |
// Log | |
} | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment