I have an adapter that draws a string and a picture. This adapter is now used 5 fragments.

I read somewhere that the adapter should not be tied to a class. The adapter must carry in its name only what it does.

Therefore, I think it would be wrong to create for each fragment or activate your adapter. (If I'm wrong, then correct me).

But my whole problem is that in the adapter all interactions are used in BindView , or rather, I mean OnClickListener .

It turns out 1 fragment uses the adapter and on pressing there is one. 2 fragment does another, 3 - the third, etc.

The only option is to pass a variable to the adapter and perform a check. But, you see, it looks like something is not professional. If you remember, earlier in ListView was possible to render OnItemClickListener and all problems were solved. And what about RecyclerView ?


Here is what I understand from your answers.

 public class PhotoAlbumsAdapter extends RecyclerView.Adapter<PhotoAlbumsAdapter.ViewHolder>{ private Context context; private OnItemClickListener onItemClickListener; private String str[] = {"test", "test", "test", "test", "test", "test", "test", "test", "test", "test"}; public PhotoAlbumsAdapter(Context context) { this.context = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.adapter_album_small, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.txtTitle.setOnClickListener(onItemClickListener); } @Override public int getItemCount() { return str.length; } public void setOnItemClickListener(OnItemClickListener onItemClickListener){ this.onItemClickListener= onItemClickListener; } static class ViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.imgAlbum) ImageView imgAlbum; @BindView(R.id.txtTitle) TextView txtTitle; ViewHolder(View view) { super(view); ButterKnife.bind(this, view); } } public interface OnItemClickListener{ void onClick(); } } 

holder.txtTitle.setOnClickListener(onItemClickListener); IDEA marks in red. What to do?

    2 answers 2

    If I understand you correctly, you need to make a custom callback for the adapter.

    For example:

     public interface OnItemClickListener{ void onClick(); } 

    Next, in the adapter, visit a copy of this leaflet:

     public void setOnItemClickListener(OnItemClickListener:itemClickListener){ this.itemClickListener=itemClickListener; } 

    In the onBindViewHolder method in the adapter, simply call this callback:

     holder.tvTest.setOnClickListener(v->itemClickListener.onClick()) 

    In each of the fragments, the code will be as follows:

     adapter.setOnItemClickListener(new OnItemClickListener(){ //действие по клику }) 

    UPDATE

    You pass the wrong instance to your text field onClickListener . Do this:

     holder.txtTitle.setOnClickListener(new OnClickListener(){ onItemClickListemer.onClick() } 

    The point is that you make your own listner, which is triggered by a click on the text field.

    • this.itemClickListener Create a private instance of OnItemClickListener in the Adapter? - user239760
    • Yes that's right. Private instance and setter to it - no news
    • holder.txtTitle.setOnClickListener(onItemClickListener); setOnClickListener (android.view.View.OnClickListener) cannot be applied to (adapters.PhotoAlbumsAdapter.OnItemClickListener) - user239760
    • Update the question, and show the full code of the adapter and one of the fragments - no news
    • one
      @EraNewGames before writing this solution, I checked it in the development environment, I was fine. Apparently you did something wrong, and it is much more profitable for you to pass the blame on to other people. In any case, I am glad that you understand, we can finish this conversation. Good luck - no news

    Create an adapter

     private View.OnClickListener onClickListener; @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.txtTitle.setOnClickListener(onClickListener); } public void setOnItemClickListener(View.OnClickListener onClickListener){ this.onClickListener=onClickListener; } 

    Now after creating an adapter instance, this adapter will have a setOnItemClickListener method. Call it and inside describe all our actions.

     photoAlbumsAdapter.setOnItemClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("GroupFragment=onClick", "TESA" + ""); } });