Created
April 25, 2021 10:10
-
-
Save varundwarkani/4abcf0e762f2acc587e5176da9db5d06 to your computer and use it in GitHub Desktop.
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
public class UpcomingMatchesViewAdapter extends PagedListAdapter<UpcomingMatch, UpcomingMatchesViewAdapter.GenericViewHolder> { | |
private static final String LOGGER = UpcomingMatchesViewAdapter.class.getName(); | |
private HomeScreenViewModel homeScreenViewModel; | |
public void setRecyclerViewViewModel(HomeScreenViewModel homeScreenViewModel) { | |
this.homeScreenViewModel = homeScreenViewModel; | |
} | |
public UpcomingMatchesViewAdapter() { | |
super(DIFF_CALLBACK); | |
} | |
@NonNull | |
@Override | |
public GenericViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); | |
ItemMatchBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.item_match, parent, false); | |
return new GenericViewHolder(binding); | |
} | |
@Override | |
public void onBindViewHolder(@NonNull GenericViewHolder holder, int position) { | |
holder.bind(homeScreenViewModel, position); | |
holder.binding.setUpcomingMatch(getItem(position)); | |
} | |
private static DiffUtil.ItemCallback<UpcomingMatch> DIFF_CALLBACK = | |
new DiffUtil.ItemCallback<UpcomingMatch>() { | |
@Override | |
public boolean areItemsTheSame(UpcomingMatch oldItem, UpcomingMatch newItem) { | |
return oldItem.getMatchid() == newItem.getMatchid(); | |
} | |
@Override | |
public boolean areContentsTheSame(UpcomingMatch oldItem, UpcomingMatch newItem) { | |
return oldItem.getMatchid() == newItem.getMatchid(); | |
} | |
}; | |
class GenericViewHolder extends RecyclerView.ViewHolder { | |
final ItemMatchBinding binding; | |
GenericViewHolder(ItemMatchBinding binding) { | |
super(binding.getRoot()); | |
this.binding = binding; | |
} | |
void bind(HomeScreenViewModel homeScreenViewModel, Integer position) { | |
binding.setVariable(BR.homeScreenViewModel, homeScreenViewModel); | |
binding.setVariable(BR.position, position); | |
binding.executePendingBindings(); | |
} | |
} | |
@Override | |
public int getItemCount() { | |
return super.getItemCount(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment