1

I'm new on Android material design and wanna use Recycle View instead of List View but having problem on implement OnClickListener. I find out that RecyclerView.Adapter a bit different from ListView.Adapter. first I impelement OnClick listener in onBindViewHolder but it returns wrong item numbers and lead to out of-bound after some removing.

 @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
 final int index = i ;
personViewHolder.cv.setOnClickListener(new View.OnClickListener() {
                    @Override
                   public void onClick(View v) {
    ...           items.get(index).gettext();
    }

so I find out that I should implement OnClikListener in my ViewHolder class as this link.

but in this case never inter to OnClick.

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

    @Override
    public void onClick(View v) {

        Log.i("log","position="+getAdapterPosition());

    }
}

 public CardViewAdapter(List<MessageTO> persons) {
        this.items = persons;
    }


@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);

}

@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.deposite_card_view, viewGroup, false);
    PersonViewHolder pvh = new PersonViewHolder(v);
    return pvh;
}

@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
...
}

also I find out that I should do my Initializing item values all in onBindViewHolder is it right or not? I am very confused.

Mahdi
  • 6,139
  • 9
  • 57
  • 109
  • Possible duplicate of [RecyclerView onClick](http://stackoverflow.com/questions/24471109/recyclerview-onclick) – random Dec 28 '15 at 13:37
  • @random I check that. it's not happening. – Mahdi Dec 28 '15 at 13:38
  • I see that you're using `PersonViewHolder` in onBindViewHolder. But you're checking `onClick` in a `ViewHolder` class. Please see that you're adding `onClick` to correct view holder class that is used in your recycler view – random Dec 28 '15 at 13:41
  • @random I copy paste the link code in second part. Ok I will update question – Mahdi Dec 28 '15 at 13:42
  • Also post your `onCreateViewHolder` and what elements you have in your view holder and what are you clicking – random Dec 28 '15 at 13:45
  • @random ok, I updated the question. – Mahdi Dec 28 '15 at 13:53
  • follow this it has everything you need for recycler view http://www.androidhive.info/2015/04/android-getting-started-with-material-design/ – Anoop Kanyan Dec 28 '15 at 13:53

2 Answers2

1

First you need to set the onclicklistener as shown below. Then it should work.

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

public ViewHolder(View itemView) {
        super(itemView);
        .
        .
        .
        itemView.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    Log.i("log","position="+getAdapterPosition());

}

}

Nikhil Gupta
  • 881
  • 6
  • 14
0

You need to call viewHolder.itemView.setOnClickListener(viewHolder) somewhere to make it work

aelimill
  • 1,015
  • 1
  • 10
  • 17
  • You implement the onClickListener but never attach it to the viewHolder view itself. – aelimill Dec 28 '15 at 14:51
  • when it's implements an event it not need to asigning. – Mahdi Dec 28 '15 at 15:26
  • You're wrong. You need to assign the OnClickListener to some sort of view to make it works. In the link that you provided it is attached in viewholder constructor (public ViewHolder(View view) { super(view); view.setOnClickListener(this); mTextView = (TextView) view; }) , but there is no assignment in your code. – aelimill Dec 28 '15 at 15:57