0
votes

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?

2

2 Answers

0
votes

Have all of the views inherit from a common abstract parent that has those clickable views attach the click listeners.

abstract class CommonViewHolder {
  TextView title;
  TextView desc;
  ImageButton menuButton;
  CommonViewHolder() {
    title.setOnClickListener(...);
    desc.setOnClickListener(...);
    menuButton.setOnClickListener(...);
  } 
}

class ViewHolderMain extends CommonViewHolder {
...
}

class ViewHolderDefault extends CommonViewHolder {
...
}

class ViewHolderCustom extends CommonViewHolder {
...
}
0
votes

You can add android:onClick="activityOnClick" in the XML layout for all relevant views; then the public void activityOnClick(View view) method of the wrapping Activity will be called.