Last active
March 29, 2019 06:42
-
-
Save sab99r/78632e2d9740d32ddec96e40ea9daf8a to your computer and use it in GitHub Desktop.
RecyclerView Load More
This file contains hidden or 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
| public class MoviesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | |
| public final int TYPE_MOVIE = 0; | |
| public final int TYPE_LOAD = 1; | |
| static Context context; | |
| List<MovieModel> movies; | |
| OnLoadMoreListener loadMoreListener; | |
| boolean isLoading = false, isMoreDataAvailable = true; | |
| /* | |
| * isLoading - to set the remote loading and complete status to fix back to back load more call | |
| * isMoreDataAvailable - to set whether more data from server available or not. | |
| * It will prevent useless load more request even after all the server data loaded | |
| * */ | |
| public MoviesAdapter(Context context, List<MovieModel> movies) { | |
| this.context = context; | |
| this.movies = movies; | |
| } | |
| @Override | |
| public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
| LayoutInflater inflater = LayoutInflater.from(context); | |
| if(viewType==TYPE_MOVIE){ | |
| return new MovieHolder(inflater.inflate(R.layout.row_movie,parent,false)); | |
| }else{ | |
| return new LoadHolder(inflater.inflate(R.layout.row_load,parent,false)); | |
| } | |
| } | |
| @Override | |
| public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { | |
| if(position>=getItemCount()-1 && isMoreDataAvailable && !isLoading && loadMoreListener!=null){ | |
| isLoading = true; | |
| loadMoreListener.onLoadMore(); | |
| } | |
| if(getItemViewType(position)==TYPE_MOVIE){ | |
| ((MovieHolder)holder).bindData(movies.get(position)); | |
| } | |
| //No else part needed as load holder doesn't bind any data | |
| } | |
| @Override | |
| public int getItemViewType(int position) { | |
| if(movies.get(position).type.equals("movie")){ | |
| return TYPE_MOVIE; | |
| }else{ | |
| return TYPE_LOAD; | |
| } | |
| } | |
| @Override | |
| public int getItemCount() { | |
| return movies.size(); | |
| } | |
| /* VIEW HOLDERS */ | |
| static class MovieHolder extends RecyclerView.ViewHolder{ | |
| TextView tvTitle; | |
| TextView tvRating; | |
| public MovieHolder(View itemView) { | |
| super(itemView); | |
| tvTitle=(TextView)itemView.findViewById(R.id.title); | |
| tvRating=(TextView)itemView.findViewById(R.id.rating); | |
| } | |
| void bindData(MovieModel movieModel){ | |
| tvTitle.setText(movieModel.title); | |
| tvRating.setText("Rating "+movieModel.rating); | |
| } | |
| } | |
| static class LoadHolder extends RecyclerView.ViewHolder{ | |
| public LoadHolder(View itemView) { | |
| super(itemView); | |
| } | |
| } | |
| public void setMoreDataAvailable(boolean moreDataAvailable) { | |
| isMoreDataAvailable = moreDataAvailable; | |
| } | |
| /* notifyDataSetChanged is final method so we can't override it | |
| call adapter.notifyDataChanged(); after update the list | |
| */ | |
| public void notifyDataChanged(){ | |
| notifyDataSetChanged(); | |
| isLoading = false; | |
| } | |
| interface OnLoadMoreListener{ | |
| void onLoadMore(); | |
| } | |
| public void setLoadMoreListener(OnLoadMoreListener loadMoreListener) { | |
| this.loadMoreListener = loadMoreListener; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment