25

I have a ListView that with alphabetical headers for each letter. I also have an index function that brings the letter headers to the top of the screen.

My problem is when I reach the end of the list setSelection is unable to bring the last few headers to the top because it will not scroll past the end of the list.

My question is this: Is there a way to add a blank space to the end of the screen dependent on screen size? I would like to scroll until the last item in the list is at the top of the listView.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Josh
  • 2,685
  • 6
  • 33
  • 47

3 Answers3

89

The easiest way to add space is to add padding in xml and set clipToPadding:"false".

For RecyclerView

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:clipToPadding="false"/>

For ListView

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:clipToPadding="false"/>

And same goes for the ScrollView

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:clipToPadding="false"/>

This specifically adds the blank space to top and bottom but hide the space when you scroll the view.

Community
  • 1
  • 1
Hemant Shori
  • 2,463
  • 1
  • 22
  • 20
5

Try the followings:

View footer = new View(getActivity());
footer.setLayoutParams( new AbsListView.LayoutParams( LayoutParams.FILL_PARENT, 100 ));
mListView.addFooterView(footer, null, false);
Yuwen
  • 963
  • 11
  • 8
  • 3
    what a tragedy http://stackoverflow.com/questions/13366281/how-can-i-add-blank-space-to-the-end-of-a-listview same answer with 2 downvotes – Shubham AgaRwal Oct 20 '15 at 06:40
  • Best answer is here: http://stackoverflow.com/a/13366310/2529315 with http://stackoverflow.com/a/14560982/2529315 – Johnny Nov 29 '16 at 08:05
2

I'm assuming you are using an extension of BaseAdapter to populate your ListView?

There may be a built-in way to do what you are asking, but I don't know of one. If you end up creating it yourself, how about this approach:

  • Return list.size() + EXTRA in getCount()
  • Modify getItem() to return something sane if it asks for an item not in your list
  • Modify getView() to configure the given view as a simple horizontal padding with the same height as the rest of your views if the position index is more than your list size

You would need to fiddle around with the EXTRA constant to see what value is best.

Aaron C
  • 3,328
  • 1
  • 24
  • 22
  • I disagree, @Shayan_Aryan this answer perfectly helped me to figure what to do in a matter of 30 seconds :) – Async- Jun 29 '16 at 09:16
  • only in case of using BaseAdapter, though responsibility to provide full info lays on the person asking a question, and he did not specify what he used, so it's a valid answer – Async- Jun 29 '16 at 09:41