1

I'm using a HorizontalScrollView in one of my layouts and replaced the scroll bar track and the thumb by overriding the according xml attributes, so that the track represents thumbnails of the views within the HorizontalScrollView and the thumb indicates the current position.

This works fine so far and I can scroll within the view. However, I cannot use the scroll bar, i.e. the track and the thumb for scrolling, I can only scroll via flings on top of the whole view. Every click/fling on top of the scroll bar is simply interpreted like a click/fling elsewhere in the HorizontalScrollView. In other words, I can't drag the thumb and thereby change the position, nor can I jump to a certain position by clicking somewhere on the scroll bar track.

Here is my layout:

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbarFadeDuration="0"
android:scrollbarThumbHorizontal="@drawable/highlight_box"
android:scrollbarTrackHorizontal="@drawable/tumbnail_chain" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

       ...couple of FrameLayouts

    </LinearLayout>

</HorizontalScrollView>

Unfortunately I couldn't find any solution to this problem yet

EDIT:
I just found out that the scroll bar is not supposed to work this way in Android, see here

Does anyone have an idea, how I could implement the desired behaviour explained above? No code needed, just suggestions!

EDIT 2 (Workaround):
To the ones who are interested in my solution: I put a transparent dummy view on top of the scrollbar of the same size and set an OnTouchListener to this view. In the onTouch method of the listener I then calculate the relative position of the touch event as far as the track bar is concerned and then jump to the same relative position within the HorizontalScrollView via the scrollTo method:

public boolean onTouch (View v, MotionEvent event) {
    int x = Math.round((event.getX() / mScrollbarWidth) * mScrollViewWidth);
    mScrollView.scrollTo(x, 0);
    return true;
}

where mScrollBarWidth is the width of the scrollbar (essentially the screen width) and mScrollViewWidth is the width of the HorizontalScrollView in total. Since this method is called continuously during touching the dummy view, you can not only jump to the position you touched, you can also drag the thumb on the scrollbar.

2 Answers 2

1

I think the touch event on the HorizontalScrollView is being intercepted by any parent view or layout. make an object of the HorizontalScrollView in the java file and call the following code on it -

 view.getParent().requestDisallowInterceptTouchEvent(true); //view being the object of scrollview

This might work.

1
  • I don't think the touch events are being intercepted, since the scrolling via fling does work as desired, just the scroll bar cannot be used for scrolling as one might expect it from non-android applications. Maybe I didn't quite state my question clear enough. Feb 13, 2012 at 12:44
1

i have a similar situation, a big document to display (music notation in my case) and want to use the drag the scroll bar (in addition to normal drag and fling) so the user can random access any part of the doc easily without hours of scrolling.

i think another simple solution is to look at the touch position in onScroll and onFling and if you detect you are over the scroll bar, do a manual scrollTo. i have my scrollbar set to never fade so easy for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.