Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView padding for content without moving ListView height?

I have a ListView with a bunch of items inside it. How do I make the top and bottom items have a top margin of 10dp for top item and bottom margin of 10dp for bottom item? Now I can do this with padding or margin on the ListView but the result is that when you scroll the edge of the ListView is now 10dp from the bottom of the screen. Anyway around this? I have also tried setting margins inside of the getView method of my adapter but I don't see any margin options for AbsListView.LayoutParams. Any help would be great.

Thanks

like image 465
Bobbake4 Avatar asked Oct 24 '11 21:10

Bobbake4


3 Answers

The trick is to include android:clipToPadding="false" in your view definition, e.g.:

<ListView android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="16dip"
    android:paddingBottom="16dip"
    android:clipToPadding="false" />
like image 158
adamp Avatar answered Nov 14 '22 05:11

adamp


You can get space at the top and bottom of your ListView, inside the scrolling region, by using headers and footers. Here's a quick example:

Define your filler view, which I named header_footer.xml:

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="10dp"/>

Now your onCreate method looks like this, assuming you named your ListView listView:

    final LayoutInflater inflater = 
        (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View headerFooter = inflater.inflate(R.layout.header_footer, null);
    listView.addFooterView(headerFooter);
    listView.addHeaderView(headerFooter);

And then set your adapter.

like image 43
skynet Avatar answered Nov 14 '22 04:11

skynet


Why not just check the position of the item relative to the size of your list:

Padding:

 public View getView(int position, View convertView, ViewGroup parent)
    {
         //recycle views and whatever else you normally would do goes here..
         //...
         //...

         if (position == 0){
            convertView.setPadding(0, 10, 0, 0); //padding on top for top item
         }
         else if (position == getCount() - 1){
            convertView.setPadding(0, 0, 0, 10); //padding on bottom for bottom item
         }
         else{
            convertView.setPadding(0, 0, 0, 0); //no padding
         }
}

For margins use

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
convertView.setLayoutParams(lp);

Ex:

public View getView(int position, View convertView, ViewGroup parent)
    {
             //recycle views and whatever else you normally would do goes here..
             //...
             //...
             LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,        LinearLayout.LayoutParams.WRAP_CONTENT);
             if (position == 0){
                 lp.setMargins(0, 10, 0, 0); //margin on top for top item
             }
             else if (position == getCount() - 1){
                lp.setMargins(0, 10, 0, 10); //margin on bottom for bottom item
             }
             else{
                lp.setMargins(0, 0, 0, 0); //no margin
             }
             convertView.setLayoutParams(lp);
    }

this is provided you've implemented the getCount() method properly for your adapter.

like image 5
dymmeh Avatar answered Nov 14 '22 06:11

dymmeh