I want to build a RecyclerView with a few columns within each row that should be clickable to different events. Unfortunately, I haven't been able to determine which onclick is being clicked within the row, as they all call the same method. What's the best way to handle this? Should I declare multiple views within the row and assign an onclick to each View?
Here is my 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 is my recyclerview holder code (normal recyclerview). I want to replace the onLongClick event with a 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;
for (int i = 0; i < 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() + " was accepted");
} else {
mVerifys.get(i).setAccept("N");
mVerifys.get(i).setSelected(false);
Timber.d(verificationModel.getDescription() + " was 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() + " will be adjusted");
Intent intent = new Intent(VerificationDetailActivity.this, InvoiceAuthorizationActivity.class);
startActivity(intent);
return true;
}
}