![](/img/trans.png)
[英]Android ListView Footer View not being placed on the bottom of the screen
[英]Listview - Footer at the bottom of screen
我有一個ListView添加了listview.addFooterView(footerView);
所有工作都按預期排除在一種情況下:當我的listview的項目沒有填滿整個屏幕時,我希望頁腳位於屏幕的底部,而不是在中間。 有沒有辦法輕松做到這一點? 或者我應該改變我的布局?
謝謝
編輯:這可能會有所幫助(這就是我想要的)
如果你想讓它始終位於屏幕的底部,無論你的ListView
有多長,那么擺脫listview.addFooterView(footerView);
並使用RelativeLayout. Give your
RelativeLayout. Give your
ListView`屬性
android:layout_alignParentTop="true"
並將屬性提供給您的頁腳
android:layout_alignParentBottom="true"
如果這不能解決您的問題,那么請更具體地了解您的需求,並盡可能提供您想要的圖片。
編輯
閱讀完評論后,這可能有用。 可能有一種更簡單的方法,但你可以做類似的事情
listView.post(new Runnable()
{
public void run()
{
int numItemsVisible = listView.getLastVisiblePosition() -
listView.getFirstVisiblePosition();
if (itemsAdapter.getCount() - 1 > numItemsVisible)
{
// set your footer on the ListView
}
else
{
footerView.setVisibility(View.VISIBLE);
}
}
footerView
將是一個自定義layout
,您將使用上面引用的屬性創建。 如果項目不能超過屏幕,則應將其設置為visible
。 如果它們超出了適合的范圍,那么就像現在一樣在ListView
上應用頁腳視圖。 這可能不是最好的方式,但它首先浮現在腦海中。 您可以在設置Adapter
之前運行此代碼。
您不能將ListView頁腳用作整個布局的頁腳。 最好使用RelativeLayout作為布局的根元素,然后使用屬性的直接子元素包含頁腳視圖:android:layout_alignParentBottom =“true”
除了@codeMagic響應之外,您還可以添加一個偵聽器來檢查適配器何時更新,然后更新頁腳
registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
updateSmartFooter();
}
});
其中updateSmartFooter是他描述的功能
private void updateSmartFooter {
listView.post(new Runnable()
{
public void run()
{
int numItemsVisible = listView.getLastVisiblePosition() -
listView.getFirstVisiblePosition();
if (itemsAdapter.getCount() - 1 > numItemsVisible)
{
// set your footer on the ListView
}
else
{
footerView.setVisibility(View.VISIBLE);
}
}
}
}
花了很多時間研究之后,我找到了最好的解決方案。 請查看: https : //stackoverflow.com/a/38890559/6166660和https://github.com/JohnKuper/recyclerview-sticky-footer
詳情如下:
recyclerListView.addItemDecoration(new StickyFooterItemDecoration());
---------------------------------------
public class StickyFooterItemDecoration extends RecyclerView.ItemDecoration {
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)) {
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;
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.