我想在列表视图中添加页脚。当列表项的数量更多时,页脚工作正常。 但是当listview的项目很少时,页脚会显示在屏幕中间,就在listview的下方。它看起来很破旧。在这种情况下,我希望页脚对齐父底部。 谢谢你的期待。
答案 0 :(得分:0)
也许你的listview的高度设置为wrap_content?
据我所知,页脚添加在列表视图的底部。如果将listview的高度设置为match_parent,则应将其与底部对齐,并且还应在其中显示页脚。 (如果使用相对布局,只需在.xml文件中设置listview的属性alignParentBottom =“true”)
答案 1 :(得分:0)
这是你想要的最简单的例子。你可以自定义它:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/footer"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="90dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:id="@+id/footer" >
</RelativeLayout>
</RelativeLayout>
如果你想这样做,你在评论中说,你必须在代码中设置页脚的布局参数,你必须得到列表的大小,然后得到屏幕上显示的行数,然后
if (listSize < numRow)
//set footer to bottom of your list
else
// android:layout_alignParentBottom="true"
答案 2 :(得分:0)
您可以将RecyclerView与RecyclerView.ItemDecoration一起使用来实现此行为。
public class StickyFooterItemDecoration extends RecyclerView.ItemDecoration {
/**
* Top offset to completely hide footer from the screen and therefore avoid noticeable blink during changing position of the footer.
*/
private static final int OFF_SCREEN_OFFSET = 5000;
@Override
public void getItemOffsets(Rect outRect, final View view, final RecyclerView parent, RecyclerView.State state) {
int adapterItemCount = parent.getAdapter().getItemCount();
if (isFooter(parent, view, adapterItemCount)) {
//For the first time, each view doesn't contain any parameters related to its size,
//hence we can't calculate the appropriate offset.
//In this case, set a big top offset and notify adapter to update footer one more time.
//Also, we shouldn't do it if footer became visible after scrolling.
if (view.getHeight() == 0 && state.didStructureChange()) {
hideFooterAndUpdate(outRect, view, parent);
} else {
outRect.set(0, calculateTopOffset(parent, view, adapterItemCount), 0, 0);
}
}
}
private void hideFooterAndUpdate(Rect outRect, final View footerView, final RecyclerView parent) {
outRect.set(0, OFF_SCREEN_OFFSET, 0, 0);
footerView.post(new Runnable() {
@Override
public void run() {
parent.getAdapter().notifyDataSetChanged();
}
});
}
private int calculateTopOffset(RecyclerView parent, View footerView, int itemCount) {
int topOffset = parent.getHeight() - visibleChildsHeightWithFooter(parent, footerView, itemCount);
return topOffset < 0 ? 0 : topOffset;
}
private int visibleChildsHeightWithFooter(RecyclerView parent, View footerView, int itemCount) {
int totalHeight = 0;
//In the case of dynamic content when adding or removing are possible itemCount from the adapter is reliable,
//but when the screen can fit fewer items than in adapter, getChildCount() from RecyclerView should be used.
int onScreenItemCount = Math.min(parent.getChildCount(), itemCount);
for (int i = 0; i < onScreenItemCount - 1; i++) {
totalHeight += parent.getChildAt(i).getHeight();
}
return totalHeight + footerView.getHeight();
}
private boolean isFooter(RecyclerView parent, View view, int itemCount) {
return parent.getChildAdapterPosition(view) == itemCount - 1;
}
}
确保为RecyclerView高度设置 match_parent 。
请查看示例应用https://github.com/JohnKuper/recyclerview-sticky-footer及其工作原理http://sendvid.com/nbpj0806
此解决方案的一个巨大缺点是它只能在整个应用程序(不在装饰内)中的notifyDataSetChanged()之后正常工作。通过更具体的通知,它无法正常工作并支持它们,它需要更多逻辑。此外,您可以通过 eowise 从图书馆 recyclerview-stickyheaders 获取见解,并改进此解决方案。