android - RecyclerView with multiple onclick events on textviews -


i want build recyclerview few columns within each row should clickable different events. unfortunately, haven't been able determine onclick being clicked within row, call same method. what's best way handle this? should declare multiple views within row , assign onclick each view?

here recyclerview.xml file:

<relativelayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="wrap_content">      <relativelayout         android:id="@+id/layout"         android:layout_width="match_parent"         android:layout_height="wrap_content">          <textview             android:text="description"             android:id="@+id/description_textview"             android:layout_width="180dp"             android:layout_height="wrap_content"             android:textsize="18sp"             android:textstyle="bold"             android:textcolor="@color/colorblack"             android:paddingleft="4dp"             />          <textview             android:text="99"             android:id="@+id/qty_textview"             android:layout_width="60dp"             android:layout_height="wrap_content"             android:textsize="18sp"             android:textstyle="bold"             android:textcolor="@color/colorblack"             android:layout_torightof="@id/description_textview"             android:gravity="center"             />          <textview             android:text="n"             android:id="@+id/accept_textview"             android:layout_width="80dp"             android:layout_height="wrap_content"             android:textsize="18sp"             android:textstyle="bold"             android:textcolor="@color/colorblack"             android:layout_torightof="@id/qty_textview"             android:gravity="center"             />      </relativelayout>      <view         android:layout_width="match_parent"         android:layout_height="1dp"         android:background="@color/colorbackgroundgray"         android:layout_below="@id/layout"         style="?android:listseparatortextviewstyle"         />  </relativelayout> 

here recyclerview holder code (normal recyclerview). want replace onlongclick event different column:

private class recyclerholder2 extends recyclerview.viewholder implements view.onclicklistener, view.onlongclicklistener {     private textview descriptiontextview;     private textview qtytextview;     private textview accepttextview;     private verificationmodel verificationmodel;      public recyclerholder2(view itemview) {         super(itemview);         itemview.setonclicklistener(this);         itemview.setonlongclicklistener(this);         descriptiontextview = (textview) itemview.findviewbyid(r.id.description_textview);         qtytextview = (textview) itemview.findviewbyid(r.id.qty_textview);         accepttextview = (textview) itemview.findviewbyid(r.id.accept_textview);     }      public void bindrecyclerdata(verificationmodel newitem) {         verificationmodel = newitem;         descriptiontextview.settext(verificationmodel.getdescription());         qtytextview.settext(verificationmodel.getqty());         accepttextview.settext(verificationmodel.getaccept());     }      @override     public void onclick(view v) {         boolean accepted = true;         (int = 0; < mverifys.size(); i++) {             if (mverifys.get(i).equals(verificationmodel)) {                 if (mverifys.get(i).getaccept().equals("n")) {                     mverifys.get(i).setaccept("y");                     mverifys.get(i).setselected(true);                     timber.d(verificationmodel.getdescription() + " accepted");                 } else {                     mverifys.get(i).setaccept("n");                     mverifys.get(i).setselected(false);                     timber.d(verificationmodel.getdescription() + " unaccepted");                 }             }             if (mverifys.get(i).getaccept().equals("n")) {                 accepted = false;             }         }         mrecycleradapter2.notifydatasetchanged();         if (accepted) {             macceptedbutton.setenabled(true);         }      }      @override     public boolean onlongclick (view v) {         timber.d(verificationmodel.getdescription() + " adjusted");         intent intent = new intent(verificationdetailactivity.this, invoiceauthorizationactivity.class);         startactivity(intent);         return true;     } } 

right now, you're setting listeners this:

    itemview.setonclicklistener(this);     itemview.setonlongclicklistener(this); 

there 2 different ways can achieve you're looking for.

the first use same view.onclicklistener each view, , change based on clicked view's id.

public recyclerholder2(view itemview) {     super(itemview);     ...     descriptiontextview.setonclicklistener(this);     descriptiontextview.setonlongclicklistener(this);     qtytextview.setonclicklistener(this);     qtytextview.setonlongclicklistener(this);     accepttextview.setonclicklistener(this);     accepttextview.setonlongclicklistener(this);     ... }  @override public void onclick(view v) {     if (v.getid() == r.id.description_textview) {         ...     }     else if (...) {         ...     } } 

the second (and 1 recommend) use different view.onclicklistener() each view. cause write little more code, makes more obvious different clicks different things. (and if you're using retrolambda can wind omitting of boilerplate).

private view.onclicklistener descriptionlistener = new view.onclicklistener() {      @override     public void onclick(view view) {         ...     } };  private view.onclicklistener quantitylistener = new view.onclicklistener() {      @override     public void onclick(view view) {         ...     } };  public recyclerholder2(view itemview) {     super(itemview);     ...     descriptiontextview.setonclicklistener(descriptionlistener);     qtytextview.setonclicklistener(quantitylistener);     ... } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -