K
K
Konstantin Lipatov2018-07-06 13:25:47
Android
Konstantin Lipatov, 2018-07-06 13:25:47

How to hang a click handler on a RecyclerView element?

Hello everyone, I have a NavigationDraweerActivity in which a Fragment with a ViewPager is displayed, in which I display a fragment with a recycler view + card view. In the recyclerView adapter, the viewholder has an onClick method, the adapter itself implements the OnItemClickListener interface with the onItemClick method. The adapter also has a SetOnItemClickListener method to hang the handler on the cardview.

class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private CardView cv;
        private ImageView commandImageView;
        private TextView commandTextView;

        public myViewHolder(View itemView) {
            super(itemView);
            cv = itemView.findViewById(R.id.cv);
            commandImageView = itemView.findViewById(R.id.commandImageView);
            commandTextView = itemView.findViewById(R.id.commandTextView);

            cv.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }
public interface OnItemClickListener {
        public void onItemClick(View view , int position);
    }
    public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

In code, when I create recyclerVIew and adapter for it, I implement onClick method
recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        recViewAdapter = new RecViewAdapter(get_data(), getContext());
        recyclerView.setAdapter(recViewAdapter);

        recViewAdapter.SetOnItemClickListener(new RecViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //Мое действие
            }
        });

But due to the peculiarity of the view pager (it creates the current page and the next one), when the next page is created, the handler for the current page is redefined. As I understand it, this is because the PageVIew elements refer to the same recycler view. How can this be fixed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2018-07-06
@Weys

public class TransportAdapter extends RecyclerView.Adapter<TransportAdapter.TransportViewHolder> {

    public interface onClickListener{
        void onVariantClick(TransportModel model);
    }
    
    private onClickListener listener;
   
   .....
    @Override
    public void onBindViewHolder(@NonNull TransportViewHolder holder, int position) {
        final TransportModel transportModel = transportVariants.get(position);
        ....
        if(listener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onVariantClick(transportModel);
                }
            });
        }
    }
   .... бла бла бла

    public void setListener(onClickListener listener){
        this.listener = listener;
    }

    class TransportViewHolder extends RecyclerView.ViewHolder {
       .....бла бла бла
        public TransportViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}

I
Ivan0206, 2018-07-06
@Ivan0206

How hard are you doing.
Do you need to handle the card view click?
If yes, then you implement the OnClickListener ,
implement the OnClick method in the RecyclerView.ViewHolder instance and catch the card view click through its id.
Accordingly, in the Holder's constructor, you initialize the card view itself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question