我有RelativeLayout
两个听众:onTouch
& onClick
:
private RelativeLayout userItem;
userItem = (RelativeLayout) activity.findViewById(R.id.sliding_menu_userItem_rL);
userItem.setOnClickListener(userItemOnClick());
userItem.setOnTouchListener(userItemOnTouch());
/**
* Add user item reaction on click
*/
private View.OnClickListener userItemOnClick() {
Log.d(LOG_TAG, "userItemOnClick");
return new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
UserFragment mFrag = new UserFragment();
Log.d(LOG_TAG, "replace");
t.replace(R.id.fragment_container, mFrag);
t.addToBackStack(null);
menu.toggle();
t.commit();
}
};
}
/**
* Add user item reaction on touch
*/
private View.OnTouchListener userItemOnTouch() {
return new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
userItem.setBackgroundResource(android.R.color.holo_blue_dark);
break;
case MotionEvent.ACTION_UP:
userItem.setBackgroundResource(R.drawable.sliding_menu_user_item_bg);
break;
default:
break;
}
return true;
}
};
}
问题是 onClickListener 只有在这条线出现时才会做出反应:
userItem.setOnTouchListener(userItemOnTouch());
在其他情况下仅适用于 onTouchListener。我不知道为什么。请帮忙。