Butterknife: temporarily disable the listener

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

+4
source share
3 answers

I looked at ButterKnife's sources, and it looks like BK generates and sets OnClickListenerin views when you use annotation @OnClick(R.id.some_id).

Now the only way to disable it OnClickListeneris to delete it:

item.setOnClickListener(null).

, ButterKnife :

ButterKnife.inject(item);

, , , .

+3

Butterknife . , , if. if(yourboolean == true){ //set your onClickListener }else{//do nothing }

+2

In the application, you can add a boolean type flag shouldListenClickand set it to true and false, wherever you wish. You can then check shouldListenClickin @OnClickor any other call to the ButterKnife listener before executing its code.

+1
source

All Articles