0

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;
        }
blavi
  • 531
  • 1
  • 10
  • 26
  • 1
    Use this answer to get the state of scrolling http://stackoverflow.com/a/25615921/3678849 – RuNo280 May 06 '17 at 12:13
  • Hi! Thank you, it fixed my problem. I used that custom horizontalScrollVIew, checked in button onTouchListener if it's scrolling and also changed event.getAction() == MotionEvent.ACTION_DOWN to event.getAction() == MotionEvent.ACTION_UP. – blavi May 08 '17 at 10:33

0 Answers0