suresh
suresh

Reputation: 417

Differentiate touch events between customView and scroll view

I have one customView and customView added in scroll view. Now i want differentiate both touch events. My problem is when try to scroll, customView also getting touch event and when i try to change in customView, scroll view getting events.

  1. How can we stop customView touch event when scrolling.
  2. How can we stop scroll touch events when customView wants events.

Thanks in advance

Upvotes: 4

Views: 1515

Answers (2)

Rainmaker
Rainmaker

Reputation: 11100

You can set touch listener to child view and then in onTouch() event, you can block intercept touch event of parent.

You can use the same code as in https://stackoverflow.com/revisions/19311197/1

v.setOnTouchListener(new OnTouchListener() {
    // Setting on Touch Listener for handling the touch inside ScrollView
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    // Disallow the touch request for parent scroll on touch of child view
    v.getParent().requestDisallowInterceptTouchEvent(true);
    return false;
    }
});

About the second question, I don't know exactly what you're doing with customview but maybe you'd like to use click events instead because it's rather not user friendly to use different logic in ontouch and onclick as it will always fire up unexpectedly.

Upvotes: 8

Ahamed Yasin Majeeth
Ahamed Yasin Majeeth

Reputation: 30

boolean isScrolling = false;

myScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() { @Override public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) 
 isScrolling = scrollX != oldScrollX;

//for vertical scrolling

});

//then onTouchListener

if(!isScrolling){//Do operations on non scrolled state}

Upvotes: 1

Related Questions