I have a HorizontalScrollView that contains several buttons. I want to be able to tap the buttons, but they also get tapped while scrolling if the gesture happens above the buttons. I want to avoid button pressing when scrolling above them. Here's what I've done so far.
@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;
}