I have my different view holders in my RecyclerView. In onCreateViewHolder I check for viewType then return the appropriate viewHolder
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
this.mContext = parent.getContext();
switch (viewType)
{
case ITEM_TYPE_HEADER_MAIN:
return new ViewHolderHeaderMain(MainHeaderView.newInstance(parent));
case ITEM_VIEW_TYPE_DEFAULT:
return new ViewHolderDefualt(LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_list, parent, false), this.mContext);
case ITEM_VIEW_TYPE_CUSTOM:
return new ViewHolderCustom(LayoutInflater.from(parent.getContext())
.inflate(R.layout.story_custom_layout, parent, false), this.mContext);
default:
throw new IllegalArgumentException();
}
}
and:
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final StoryModel storyModel = this.mStories.get(position);
final StoryModel topStory = this.mTopStory;
if (holder instanceof ViewHolderHeaderMain) {
ViewHolderHeaderMain viewHolderHeaderMain = (ViewHolderHeaderMain) holder;
viewHolderHeaderMain.getMainHeaderView().setTopStories(this.mTopStory);
} else if (holder instanceof ViewHolderDefault){
ViewHolderDefault viewHolderDefault = (ViewHolderDefault) holder;
viewHolderDefault.bindStory(this.mStories.get(position));
} else if (holder instanceof ViewHolderCustom){
ViewHolderCustom viewHolderCustom = (ViewHolderCustom) holder;
viewHolderCustom.bindStory(this.mStories.get(position));
}
Those three viewHolders have the same Clickable views like TextView for title, description and ImageButton for overflow menu and ImageView for image. In those viewHolders I do the views to data binding (not in the onBindViewHolder).
Now, when it comes to onClickListeners, I have to implement View.onClickListener in each ViewHolder.
My question is: Is there any way to have one method for those clickable views and assign OnClickListener to each view rather repeating the same in each viewHolder?
like:
private void setupClickableViews(final View view, final ViewHolderHeaderMain viewHolderheaderMain) {
viewHolderheaderMain.tvTitle.setOnClickListener(new View.OnClickListener() {
...
Where should I define the three same methods for three view holders.
Any suggestions?