繁体   English   中英

Listview - 屏幕底部的页脚

[英]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/6166660https://github.com/JohnKuper/recyclerview-sticky-footer

详情如下:

  • 创建StickyFooterItemDecoration扩展RecyclerView.ItemDecoration ,如下面的示例代码。
  • 之后,将ItemDecoration设置为recyclerView:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM