Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add blank space to the end of a ListView?

I have a number of elements in a ListView that scroll off the screen.

I would like there to be blank space at the end of the View. That is, the user should be able to scroll past the last element such that the last element is in the middle of the viewport.

I could use an OverScroller, but I think that would only enable the View to have a bouncy effect like one often sees on the iPhone.

Is there something I might have overlooked?

The scrolled-to-the-botton screen should look something like this: Android mockup where there are a number of labels the last of which is near the middle of the screen

like image 466
lfaraone Avatar asked Nov 13 '12 18:11

lfaraone


People also ask

How do I add blank space to a list in Python?

You can't leave an "empty space" in a list; each index has to contain something. In general, an "empty" index would be indicated by some sentinel value, often None .

What does ListView uses to place each item?

ListView uses Adapter classes which add the content from data source (such as string array, array, database etc) to ListView.

What is ListView explain it using appropriate examples?

Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.


2 Answers

The accepted answer is too complicated, and addFooterView is not for this kind of thing. The proper and simpler way is to set the paddingTop and paddingBottom, and you need to set clipToPadding to "false". In your list view or grid view, add the following:

    android:paddingTop="100dp"
    android:paddingBottom="100dp"
    android:clipToPadding="false"

You'll get blank space at the top and the bottom that moves with your finger scroll.

like image 162
X.Y. Avatar answered Sep 21 '22 20:09

X.Y.


  1. Inflate any layout of your choice (this could be an XML of and ImageView with no drawable and with set height and width of your choice)
  2. Measure the screen height and create new LayoutParams and set the height of it to 1/2 of the screen height
  3. Set the new layout params on your inflated view
  4. Use the ListView's addFooterView() method to add that view to the bottom of your list (there is also an addHeaderView())

Code to measure screen height

 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
 Display display = wm.getDefaultDisplay();
 int screenHeight = display.getHeight();

Code to set half screen height:

 View layout = inflater.inflate(R.layout.mylistviewfooter, container, false);
 ViewGroup.LayoutParams lp = layout.getLayoutParams();
 lp.height = screenHeight/2;
 layout.setLayoutParams(lp);
 myListView.addFooterView(layout);

An Aside: When you add a footer or header view to any listview, it has to be done before adding the adapter. Also, if you need to get your adapter class after doing this you will need to know calling the listview's adapter by getAdapter() will return an instance of HeaderViewListAdapter in which you will need to call its getWrappedAdapter method Something like this :

 MyAdapterClassInstance myAdapter = (MyAdapterClassInstance) ((HeaderViewListAdapter) myListView.getAdapter()).getWrappedAdapter();
like image 31
petey Avatar answered Sep 19 '22 20:09

petey