I'm looking the answer how to make onClickListener button located in Recycleview Item using Butterknife. I know how to do it without Butterknife, but I can't find anything with Bt. Does Bt supports this?
Asked
Active
Viewed 151 times
-2
-
Please provide some code so that we can understand exactly what you doing and help you with this. – Ümañg ßürmån Sep 03 '18 at 12:49
-
use BK to bind the listener in the RV viewholder, then propagate the event to the activity. Do not try to bind it directly in the activity – Tim Sep 03 '18 at 12:51
-
Do you know any example? – user9897182 Sep 03 '18 at 12:57
2 Answers
0
Try to learn from the sample code at official site If you want to implement the click logic in the activity then here are the steps.
1 Create an interface
public interface ClickHandler{
void onClick(int position);
}
2 Implement CLickHandler in activity
MainActivity extends AppCompatActivity implements ClickHandler{
...
public void onclick(int position){
Log.d("Check","Clicked at" + position);
}
...
adapter = new MyAdapter(this);//Pass the reference to activity as it implements the clickhandler.
...
}
3 Now your adapter has the reference for clickhandler. Similarly pass it to the viewholder and call the onCLick method from there.
class ViewHolder {
@BindView(R.id.title) TextView name;
@BindView(R.id.job_title) TextView jobTitle;
ClickHandler clickHandler;
public ViewHolder(View view, ClickHandler) {
ButterKnife.bind(this, view);
this.clickHandler = clickHandler;
}
@OnClick(R.id.submit)
public void submit(View view) {
if(clickHandler(!=null){
clickHandler.onClick(getAdapterPosition());
}
}
}

Jitendra A
- 1,568
- 10
- 18
0
' class ViewHolder {
@BindView(R.id.title) TextView name;
@BindView(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
@OnClick(R.id.submit)
public void submit(View view) {
// TODO submit data to server...
}
}'

Muhammad Saqib Javed
- 143
- 2
- 12