0

I have a client who wants a conventional scrollbar for ListViews in their application. The reason why is because this application will be used exclusively with their devices which happen to feature resistive screens. The screens don't track gestures very well without a stylus and a stylus will not always be available to the user. Hence the conventional scrollbar as a fallback UI control. A fast scroll thumb will not be a useful solution to this problem.

What I have found in the API that is of some use are xml attributes which allow me to approximate the behaviour of the track and thumb such as:

android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="insideInset"
android:fadeScrollbars="false"

But I haven't found anything in the API that would take care of including scroll buttons at the top and bottom of the track.

Am I correct in concluding that the API does not provide this functionality? If so, my only option would be to use a couple of buttons that change the list position. This is a doable solution of course but I wanted to make sure that I wasn't reinventing yet another wheel.

I have to support as far back as 2.2.

Scrollbar Example:

Scrollbar example

Community
  • 1
  • 1
TJ Thind
  • 784
  • 5
  • 17

3 Answers3

2

Am I correct in concluding that the API does not provide this functionality?

Yes, you are. The API does not support buttons for ListView scrolling. You'll have to implement this functionality yourself.

Edit: A quick example based on the comments below, extending ListView to support adding of buttons.

private void init() { // Call this after the ListView is in your existing layout, perhaps in onLayout?
    // The following two lines create a new LinearLayout. If you want to design the layout in XML, you'll have to use LayoutInflater.
    LinearLayout newLayout = new LinearLayout(getContext());
    newLayout.setOrientation(LinearLayout.HORIZONTAL);
    newLayout.setLayoutParams(getLayoutParams());

    ViewGroup parent = (ViewGroup) getParent();
    int index = parent.indexOfChild(this);
    parent.removeView(this);
    newLayout.addView(this);
    parent.addView(newLayout, index);

    // Now, you can customize your LinearLayout.
    newLayout.addView(...); // Maybe add a vertical LinearLayout to contain your two buttons stacked upon each other?
}

This is just a rough concept, but it should get you going.

Cat
  • 66,919
  • 24
  • 133
  • 141
  • Do you know if it would be possible for me to extend ListView to include scroll buttons? – TJ Thind Jul 18 '12 at 21:36
  • I've never done it myself, but you could try creating an extended `ListView` class. After it's initialized, [replace the `ListView`](http://stackoverflow.com/questions/3334048/android-layout-replacing-a-view-with-another-view-on-run-time) with a `LinearLayout`, and add the `ListView` to that layout. Then, in that same code, you can edit that layout to contain the buttons you need. I'll mock up a quick pseudocode of it and add it to my post... – Cat Jul 18 '12 at 21:41
  • Thanks for the confirmation and pointers. I'll put a prototype on my to-do stack. If I come up with something good I'll come back and edit my question with the solution. – TJ Thind Jul 18 '12 at 22:20
0

Paul answer is the right answer There is a thing called Fast Scroll which kind of does what you want.

You can enable it via XML:

android:fastScrollEnabled="true" Or in code:

getListView().setFastScrollEnabled(true); It's as close as it can get to what you want (without using custom views).

EDIT:

I forgot to mention that this will only work when there is enough elements on the list. It has to be around 4 times the length of what is currently visible on screen.

M.Ali El-Sayed
  • 1,608
  • 20
  • 23
-1

Eric is right... almost.

There is a thing called Fast Scroll which kind of does what you want.

You can enable it via XML:

android:fastScrollEnabled="true"

Or in code:

getListView().setFastScrollEnabled(true);

It's as close as it can get to what you want (without using custom views).

EDIT:

I forgot to mention that this will only work when there is enough elements on the list. It has to be around 4 times the length of what is currently visible on screen.

Paul
  • 5,163
  • 3
  • 35
  • 43