gpt4 book ai didi

android - 是否可以隐藏 RecyclerView 中最后一项的 ItemDecoration?

转载 作者:太空宇宙 更新时间:2023-11-03 13:11:10 25 4
gpt4 key购买 nike

我正在使用 RecyclerView.ItemDecoration类在列表中创建分隔线,但我想隐藏列表中最后一项的分隔线。这是否可能无需自己实现分隔线?

最佳答案

你可以试试这个,

public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;

public SimpleDividerItemDecoration(Context context) {
mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();

int childCount = parent.getAdapter().getItemCount();
for (int i = 0; i < childCount; i++) {

if (i == (childCount - 1)) {
continue;
}

View child = parent.getChildAt(i);

RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();

mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}

line_divider.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="1dp"
android:height="1dp" />
<solid android:color="#F5F5F5" />
</shape>

关于android - 是否可以隐藏 RecyclerView 中最后一项的 ItemDecoration?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44708457/

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