向 ListView 最后一个元素添加底部边距

我需要添加添加复杂项目背景的 ListView: 不同的偶数/奇数和圆角在顶部和底部。它看起来像这样:

ListView top

我已经通过 level-list 实现了所有这些东西,但是还有一件事我想做。 现在底部的项目接近屏幕的底部。最好添加一些空间。

ListView bottom looks not good

我不想添加底部页边距到 ListView,我只需要最后一个项目的页边距。

我认为这样做的方式是:

福特

一种将空 TextView 添加到 ListView 的 hack-add 页脚。但是页脚是相当不稳定的东西,它们通常在 notifyDataSetChanged 之后消失,而且无法恢复它们

具有透明像素的图像

我要求设计师添加透明像素底部的背景资源。不幸的是,在这种情况下,垂直居中是完全打破。 例如,有这样的9个补丁:

9patch

布局如下:

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- View with background with transparent pixels on bottom -->
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="@+id/item"
android:background="@drawable/some_bgr"
android:padding="10dp"
>
<TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"
android:text="Title"
android:layout_gravity="center"
android:textSize="18sp"
/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Detail"
android:layout_gravity="center"
android:textSize="18sp"
/>
</LinearLayout>


<!-- Just for marking place took by view -->
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_below="@id/item"
android:background="#88ff55"
/>
</RelativeLayout>

结果是:

Result

正如你所看到的,定心不起作用,很不幸。 (顺便说一句,如果指定这个9补丁作为 TextView 的背景,居中很好。如果你知道任何文章,解释这一点,请让我知道。)

在 Adapter 实现中将底部边距添加到最后一项

应该可以,但是不知道为什么我还是不能修好。 我不喜欢这种方式,因为我不喜欢在代码中修改维度。

那么

已经有一种想象的方法——用特定的位图和边距构造一些可绘制的 XML。根据可绘制的概念,这应该是可能的,但我无法找到实现。也许有人知道?

还有别的办法吗?

28218 次浏览

I guess you want to add margin only to last item:

So you can do in this manner, in your getview method the index of the list item and check if its the last item, then progrmatically add margin to the view.

In your ListView, set a paddingBottom and clipToPadding="false".

  <ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay"/>
  • This also works for RecyclerView.

  • Only use android:scrollbarStyle="outsideOverlay" if you want the scroll bar to not overflow into the padded area.

you can also do it from code if you want, for example here I react to to EditText different situations:

   if(s.toString().length()>0)
{
contacts_lv.setClipToPadding(false);
contacts_lv.setPadding(0,0,0,270*screenDensity);
}
else
{
contacts_lv.setClipToPadding(true);
contacts_lv.setPadding(0,0,0,0);
}

Clocksmith's answer is the best and pretty clever. You can also create an empty footer view.

add an empty footer in your list like this:

TextView empty = new TextView(this);
empty.setHeight(150);
listview.addFooterView(empty);

Add these two lines in your listView XML code:

android:transcriptMode="alwaysScroll"
android:stackFromBottom="true"

Another solution might be that you make a mock view with certain height.

In your adapter in getViewCount return 2.

In getCount return yourData.size+1.

In getViewType check if the element is last element return 2;

Use this type in getView to populate the mockview.