LinearLayout OnClickListener not responding
I'm creating a custom widget by expanding LinearLayout. One of the elements in my custom widget is a linear layout, inflated from ano开发者_如何转开发ther layout. When I set the OnClickListener it is not responding. Can you please advise?
Thanks!
Instead of using setOnClickListener use setOnTouchListener This code will work as a onclick event
YourLinearLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean returnValue = true;
if(event.getAction()==MotionEvent.ACTION_UP){ //on touch release
returnValue = false; //prevent default action on release
//do something here
}
return returnValue;
}
});
And then add this to your LinearLayout class to intercept child touch events
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true; //will prevent child touch events
}
Have you declared the LinearLayout clickable?
You can do this either in the XML:
android:clickable="true"
or in Java code:
myLinearLayout.setClickable( true );
Also see the other StackOverflow thread on this question:
onClickListener on a LinearLayout
Make sure you put all childs inside LinearLayout to android:clickable="false". Than myLinearLayout.setClickable( true ) works for me.
精彩评论