RecyclerView - Is it correct to set a ClickableSpan Listener inside onBindViewHolder?

154 views Asked by At

Is it correct to set ClickableSpan listeners inside RecyclerView's onBindViewHolder? For viewHolder I would be to implement a View.OnClickListener inside the viewHolder

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

@Override
public ViewHolder(@NonNull View itemView) {
   super(itemView);
   //set viewHolders
}

@Override
    public void onClick(View view) {
        //Set on click listener
    }
}

However ClickableSpan is a class. But I am unsure if recreating a new clickable span each time in onBindViewHolder is the correct way to do it.

 @Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
    //holder.viewHolderUsername.setText(list.get(position).getPosted_by_user());
    holder.viewHolderMessage.setText(list.get(position).getMessage());

    SpannableString ss = new SpannableString(list.get(position).getPosted_by_user());

    String word = list.get(holder.getAdapterPosition()).getPosted_by_user();
    final int startingPosition = word.indexOf(word);
    final int endingPosition = startingPosition + word.length();

    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(@NonNull View view) {
            Toast.makeText(mContext, list.get(holder.getAdapterPosition()).getMessage(), Toast.LENGTH_SHORT).show();

        }
    };


    ss.setSpan(clickableSpan, startingPosition, endingPosition, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    holder.viewHolderUsername.setText(ss);
    holder.viewHolderUsername.setMovementMethod(LinkMovementMethod.getInstance());

    }
0

There are 0 answers