Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Butterknife: temporarily disable listener

I set @OnClick listener to a view in a list item. In some cases I need to disable the listener for this view so that the list view can handle the click (OnItemClickListener should be called), and then enable it again. Is there a way to do this with Butterknife?

like image 618
Andrii Chernenko Avatar asked Feb 03 '15 14:02

Andrii Chernenko


3 Answers

I've looked through the ButterKnife sources and it looks like BK generates and sets OnClickListeners on views when you're using @OnClick(R.id.some_id) annotation.

Now, the only way to disable OnClickListener is to remove it:

item.setOnClickListener(null).

If you need it again, you can ask ButterKnife to repeat the injection:

ButterKnife.inject(item);

That and the solution proposed by Ethan are the only solutions I can think of.

like image 97
aga Avatar answered Nov 18 '22 20:11

aga


With Butterknife I don't know. But I know you can do this by creating a private boolean and an if statement. if(yourboolean == true){ //set your onClickListener }else{//do nothing }

like image 32
Ethan Avatar answered Nov 18 '22 18:11

Ethan


You can add a boolean flag like shouldListenClick in the app and set to true and false where ever you wish. Then, you can check the shouldListenClick in the @OnClick or any other ButterKnife listener call before executing its code.

like image 1
venkat Avatar answered Nov 18 '22 18:11

venkat