gpt4 book ai didi

android - 如何在recyclerview中实现粘性页脚

转载 作者:IT王子 更新时间:2023-10-28 23:31:27 25 4
gpt4 key购买 nike

我有 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 高度设置 ma​​tch_parent

请查看示例应用程序 https://github.com/JohnKuper/recyclerview-sticky-footer以及它是如何工作的http://sendvid.com/nbpj0806

此解决方案的一个巨大缺点是它仅在 notifyDataSetChanged() 整个应用程序(而不是内部装饰)之后才能正常工作。对于更具体的通知,它将无法正常工作并支持它们,它需要更多的逻辑。此外,您还可以从 eowise 的库 recyclerview-stickyheaders 中获得见解并改进此解决方案。

关于android - 如何在recyclerview中实现粘性页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33890709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com