Josh
Josh

Reputation: 2725

Adding Empty Space to End of ListView

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.

Upvotes: 25

Views: 11368

Answers (3)

Hemant Shori
Hemant Shori

Reputation: 2483

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.

Upvotes: 90

Yuwen
Yuwen

Reputation: 973

Try the followings:

View footer = new View(getActivity());
footer.setLayoutParams( new AbsListView.LayoutParams( LayoutParams.FILL_PARENT, 100 ));
mListView.addFooterView(footer, null, false);

Upvotes: 5

Aaron C
Aaron C

Reputation: 3313

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.

Upvotes: 2

Related Questions