gpt4 book ai didi

android - RecyclerView ItemDecoration - 如何为每个 viewHolder 绘制不同的宽度分隔线?

转载 作者:搜寻专家 更新时间:2023-11-01 09:20:12 24 4
gpt4 key购买 nike

目前我的分隔线只绘制一个宽度:

enter image description here

如何为我的 recyclerview 中的每个增量位置添加一个额外的分隔线?

这是我的 ItemDecoration 类:

public SimpleDivider(Context mContext, ArrayList<Integer> mDepth) {
mDivider = ContextCompat.getDrawable(mContext, R.drawable.recycler_view_divider);
this.mContext = mContext;
this.mDepth = mDepth;
dividerMargin = 15;

}

@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {


int top = 0;
int bottom = parent.getHeight();

int childCount = parent.getChildCount();
for(int i = 0; i < childCount; ++i) {
int right = dividerMargin;
int left = 0;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}

}

Edit1: 这是适配器。我认为不需要它,因为所有逻辑都将写在 ItemDecoration 类中。

private ArrayList<String> mList;

public class ViewHolder extends RecyclerView.ViewHolder{

TextView singleMessageComment;
public ViewHolder(@NonNull View itemView) {
super(itemView);
singleMessageComment = itemView.findViewById(R.id.item_child_comment);
}
}

public AdapterTest(ArrayList<String> mList) {
this.mList = mList;
}

@NonNull
@Override
public AdapterTest.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view_single_layout, viewGroup, false);

return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull AdapterTest.ViewHolder viewHolder, int i) {
viewHolder.singleMessageComment.setText(mList.get(i));


}

@Override
public int getItemCount() {
return mList.size();
}

最佳答案

添加装饰:

recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
recyclerView.addItemDecoration(new LeftDividerItemDecorator(this));

左分隔项装饰器的声明:

public class LeftDividerItemDecorator extends RecyclerView.ItemDecoration {
private final Drawable mDivider;
private final Rect mBounds = new Rect();
private final Context mContext;

LeftDividerItemDecorator(Context context) {
mContext = context;
mDivider = context.getResources().getDrawable(R.drawable.divider);
}

public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
if (parent.getLayoutManager() != null && mDivider != null) {
drawLeftDivider(c, parent);
}
}

private void drawLeftDivider(Canvas canvas, RecyclerView parent) {
canvas.save();

int childCount = parent.getChildCount();

for (int i = 0; i < childCount; ++i) {
View child = parent.getChildAt(i);
parent.getDecoratedBoundsWithMargins(child, mBounds);

int childAdapterPosition = parent.getChildAdapterPosition(child);

int left = parent.getPaddingLeft();

// Solid size according to divider.xml width
//int right = left + (mDivider.getIntrinsicWidth());

// Dynamic size according to divider.xml width multiplied by child number
int right = left + (mDivider.getIntrinsicWidth() * (childAdapterPosition + 1));

int top = child.getTop();
int bottom = child.getBottom();

// Draw left vertical divider
mDivider.setBounds(
left,
top,
right,
bottom
);

mDivider.draw(canvas);
}

canvas.restore();
}

// Handles dividers width - move current views to right
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
if (mDivider == null) {
outRect.set(0, 0, 0, 0);
} else {
int childAdapterPosition = parent.getChildAdapterPosition(view);
outRect.set(mDivider.getIntrinsicWidth() * childAdapterPosition, 0, 0, 0);
}
}

}

分隔符的xml:

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

预览:

enter image description here

关于android - RecyclerView ItemDecoration - 如何为每个 viewHolder 绘制不同的宽度分隔线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56751398/

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