Listview - 屏幕底部的页脚

时间:2013-08-16 23:53:43

标签: android listview layout footer

我有一个添加了listview.addFooterView(footerView);

的页脚的ListView

所有工作都按预期排除在一种情况下:当我的列表视图的项目没有填满整个屏幕时,我希望页脚位于屏幕的底部,而不是在中间。有没有办法轻松做到这一点?或者我应该改变我的布局?

由于

编辑:这可能会有所帮助(这就是我想要的) enter image description here

4 个答案:

答案 0 :(得分:7)

如果您希望它始终位于屏幕的底部,无论您的ListView有多长,那么请摆脱listview.addFooterView(footerView);并使用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之前运行此代码。

答案 1 :(得分:1)

您不能将ListView页脚用作整个布局的页脚。 最好使用RelativeLayout作为布局的根元素,然后是包含具有属性的页脚视图的直接子元素: 机器人:layout_alignParentBottom = “真”

答案 2 :(得分:0)

除了@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);
            }
         }
      }
}

答案 3 :(得分:0)

花了很多时间进行研究之后,我找到了最佳的解决方案。 请看一下:https://stackoverflow.com/a/38890559/6166660https://github.com/JohnKuper/recyclerview-sticky-footer

有关详细信息:

  • 像下面的示例代码一样,创建一个 StickyFooterItemDecoration扩展RecyclerView.ItemDecoration
  • 之后,将ItemDecoration设置为您的recyclerView:

recyclerListView.addItemDecoration(new StickyFooterItemDecoration());

--------------------------------------- < / p>

 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;
    }
}