I have implemented a custom Layout that extends RelativeLayout. It displays lots of different elements that are created at run-time and is scrollable in both dimensions using scrollTo() and scrollBy(). Scrolling works and now I'd like to add the standard Android scrollbars.
Using Scrollviews ist not possible, since I need the Layout to be scrollable in 2 dimensions, so I tried to do it as described here: Android: Enable Scrollbars on Canvas-Based View
I have implemented all the compute* methods with some (bogus) values and enabled the scrollbars. But I still can't get them to show up. Any ideas what might be the problem?
There are tons of questions like this in various mailing lists and on SO, but everywhere the answer seems to be "1. call setHorizontalScrollbarEnabled(true), 2. implement all the compute* methods, 3. call awakenScrollbars()". As far as I can tell, I have done all of that and even tried to use initializeScrollbars() but nothing happens and the docs don't offer any help.
public NodeLayout(Context context) {
super(context);
setVerticalScrollBarEnabled(true);
setHorizontalScrollBarEnabled(true);
TypedArray a = context.obtainStyledAttributes(R.styleable.View);
initializeScrollbars(a);
a.recycle();
}
@Override
protected int computeHorizontalScrollExtent() {
return 5;
}
@Override
protected int computeHorizontalScrollOffset() {
return 10;
}
@Override
protected int computeHorizontalScrollRange() {
return 50;
}
@Override
protected int computeVerticalScrollExtent() {
return getHeight() / 2;
}
@Override
protected int computeVerticalScrollOffset() {
return getHeight() / 2;
}
@Override
protected int computeVerticalScrollRange() {
return getHeight();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
awakenScrollBars();
invalidate();
return true;
}
And this is how my attrs.xml file looks like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="View">
<attr name="android:background"/>
<attr name="android:clickable"/>
<attr name="android:contentDescription"/>
<attr name="android:drawingCacheQuality"/>
<attr name="android:duplicateParentState"/>
<attr name="android:fadeScrollbars"/>
<attr name="android:fadingEdge"/>
<attr name="android:fadingEdgeLength"/>
<attr name="android:fitsSystemWindows"/>
<attr name="android:focusable"/>
<attr name="android:focusableInTouchMode"/>
<attr name="android:hapticFeedbackEnabled"/>
<attr name="android:id"/>
<attr name="android:isScrollContainer"/>
<attr name="android:keepScreenOn"/>
<attr name="android:longClickable"/>
<attr name="android:minHeight"/>
<attr name="android:minWidth"/>
<attr name="android:nextFocusDown"/>
<attr name="android:nextFocusLeft"/>
<attr name="android:nextFocusRight"/>
<attr name="android:nextFocusUp"/>
<attr name="android:onClick"/>
<attr name="android:padding"/>
<attr name="android:paddingBottom"/>
<attr name="android:paddingLeft"/>
<attr name="android:paddingRight"/>
<attr name="android:paddingTop"/>
<attr name="android:saveEnabled"/>
<attr name="android:scrollX"/>
<attr name="android:scrollY"/>
<attr name="android:scrollbarAlwaysDrawHorizontalTrack"/>
<attr name="android:scrollbarAlwaysDrawVerticalTrack"/>
<attr name="android:scrollbarDefaultDelayBeforeFade"/>
<attr name="android:scrollbarFadeDuration"/>
<attr name="android:scrollbarSize"/>
<attr name="android:scrollbarStyle"/>
<attr name="android:scrollbarThumbHorizontal"/>
<attr name="android:scrollbarThumbVertical"/>
<attr name="android:scrollbarTrackHorizontal"/>
<attr name="android:scrollbarTrackVertical"/>
<attr name="android:scrollbars"/>
<attr name="android:soundEffectsEnabled"/>
<attr name="android:tag"/>
<attr name="android:visibility"/>
</declare-styleable>
</resources>
I am developing for Android 3.0.