简体   繁体   English

Listview - 屏幕底部的页脚

[英]Listview - Footer at the bottom of screen

I have a ListView with a footer added with listview.addFooterView(footerView); 我有一个ListView添加了listview.addFooterView(footerView);

All works as expected excepted in one case: when my listview's items doesn't fill the whole screen, I would like the footer to be at the bottom of the screen, instead of being in the middle. 所有工作都按预期排除在一种情况下:当我的listview的项目没有填满整个屏幕时,我希望页脚位于屏幕的底部,而不是在中间。 Is there a way to do this easily? 有没有办法轻松做到这一点? Or should I change my layout? 或者我应该改变我的布局?

Thanks 谢谢

EDIT: that might help (this is what I want) 编辑:这可能会有所帮助(这就是我想要的) 在此输入图像描述

If you want it to always be at the bottom of the screen, no matter how long your ListView is, then get rid of listview.addFooterView(footerView); 如果你想让它始终位于屏幕的底部,无论你的ListView有多长,那么摆脱listview.addFooterView(footerView); and use a RelativeLayout. Give your 并使用RelativeLayout. Give your RelativeLayout. Give your ListView` the property RelativeLayout. Give your ListView`属性

 android:layout_alignParentTop="true"

and give the property to your footer 并将属性提供给您的页脚

 android:layout_alignParentBottom="true"

If this doesn't solve your problem then please be a little more specific about what you want and provide a picture of what you want if possible. 如果这不能解决您的问题,那么请更具体地了解您的需求,并尽可能提供您想要的图片。

Edit 编辑

After reading the comments this might work. 阅读完评论后,这可能有用。 There might be an easier way but you could do something like 可能有一种更简单的方法,但你可以做类似的事情

     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 would be a custom layout that you would create with the properties I referenced above. footerView将是一个自定义layout ,您将使用上面引用的属性创建。 This should set that to visible if the items aren't more than can fit on the screen. 如果项目不能超过屏幕,则应将其设置为visible If they are more than can fit then you apply the footer view on the ListView as you are now. 如果它们超出了适合的范围,那么就像现在一样在ListView上应用页脚视图。 This might not be the best way but its the first thing that comes to mind. 这可能不是最好的方式,但它首先浮现在脑海中。 You would run this code just before you set the Adapter . 您可以在设置Adapter之前运行此代码。

You cannot use ListView footer as footer for the whole layout. 您不能将ListView页脚用作整个布局的页脚。 You're better off with RelativeLayout as root element for your layout, and then a direct child of it containing the footer view with the attribute: android:layout_alignParentBottom="true" 最好使用RelativeLayout作为布局的根元素,然后使用属性的直接子元素包含页脚视图:android:layout_alignParentBottom =“true”

In addition to @codeMagic response, you could add a listener to check when your adapter gets updated and then update the footer 除了@codeMagic响应之外,您还可以添加一个侦听器来检查适配器何时更新,然后更新页脚

registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                updateSmartFooter();
            }
        });

where updateSmartFooter is the function he described 其中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);
            }
         }
      }
}

After spent a lot of time to research, I found the best solution for it. 花了很多时间研究之后,我找到了最好的解决方案。 Please have a look at: https://stackoverflow.com/a/38890559/6166660 and https://github.com/JohnKuper/recyclerview-sticky-footer 请查看: https//stackoverflow.com/a/38890559/6166660https://github.com/JohnKuper/recyclerview-sticky-footer

For details: 详情如下:

  • Create a StickyFooterItemDecoration extends RecyclerView.ItemDecoration like the example code below. 创建StickyFooterItemDecoration扩展RecyclerView.ItemDecoration ,如下面的示例代码。
  • After that, set ItemDecoration to your recyclerView: 之后,将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