Tuesday, January 18, 2011

Scrolling inside ScrollView

There was an issue with wheel view related to the scrolling functionality. It did not work if wheel is placed inside a ScrollView.

I've investigated standard controls and found a one that does work alike (multiline EditText) and a one working as expected (SeekBar). I would like to check also ScrollView inside a ScrollView, but fortunately I did find a solution for this issue only by using SeekBar sources from Android SDK (I have old ones, but it was enough).

The main problem was with WheelView.onTouchEvent() - it did not receive all touch events because they were processed by parent (scroll view). So it needed to ask the ScrollView to skip touch events and allow the wheel to perform them by onTouchEvent(). There is a corresponding method for a parent view called ViewParent.requestDisallowInterceptTouchEvent(). SeekBar calls it during handling the ACTION_MOVE touch event action. I've added this approach to the wheel view and it works too! :)

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (getViewAdapter() == null) {
        return true;
    }
 
    if (!gestureDetector.onTouchEvent(event) && event.getAction() == MotionEvent.ACTION_UP) {
        justify();
    }
 
    // Fix scrolling
    if (event.getAction() == MotionEvent.ACTION_MOVE && getParent() != null) {
        getParent().requestDisallowInterceptTouchEvent(true);
    }
 
    return true;
}


I'm unable to commit this fix now because of working on custom views for wheel items; will do that later. So please fix your code manually. Thanks!

7 comments:

  1. Thanks for the update Kankan! Looking forward to custom views work also!

    ReplyDelete
  2. Thanks it was helpfull

    ReplyDelete
  3. just found out about this widget - think it is amazing - great job!

    ReplyDelete
  4. Thanks for the post. Just to let you know that this didn't work until I added the following:

    @Override
    public boolean onInterceptTouchEvent(MotionEvent p_event)
    {
    return true;
    }

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Hey, I've found this while googling for a similar problem: I had a custom "seek bar" inside a scrollview.
    Pretty good advice, thanks!

    ReplyDelete
  7. Thank you! I have been looking for a solution to this problem for a while, and all answers only work for custom ViewGroup, not for View. Why? Because View doesn't have onInterceptTouchEvent.
    But your solution works, just a line of code! Thank you!!

    ReplyDelete