我有一个包含几个按钮的HorizontalScrollView。我希望能够点击按钮,但如果手势发生在按钮上方,它们也会在滚动时轻敲。我想在上面滚动时避免按下按钮。这是我到目前为止所做的。
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.interests, container, false);
interestsLayout = (LinearLayout) v.findViewById(R.id.interestsContent);
interestsHorizontalScrollView = (HorizontalScrollView) v.findViewById(R.id.horizontalScrollView);
buttons = new Button[interests.size()];
for (int i = 0; i < interests.size(); i++) {
buttons[i] = (Button) inflater.inflate(R.layout.interest_btn, interestsLayout, false);
buttons[i].setText(interests.get(i).getInterestName());
interestsLayout.addView(buttons[i]);
}
if (isSelected(interests.get(i).getInterestID())){
buttons[i].setPressed(!buttons[i].isPressed());
}
final Button btn = buttons[i];
btn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN && event.getAction() != MotionEvent.ACTION_SCROLL) {
btn.setPressed(!btn.isPressed());
}
return true;
}
});
}
return v;
}