[英]DividerItemDecoration is not applied when RecyclerView.Adapter.notifyItemInserted() is called
I created a custom divider for a RecyclerView
by extending DividerItemDecoration
and then applying it by calling addItemDecoration
on my RecyclerView.我通过扩展DividerItemDecoration
为RecyclerView
创建了一个自定义分隔DividerItemDecoration
,然后通过在我的 RecyclerView 上调用addItemDecoration
来应用它。
The RecyclerView displays everything nicely untill my data set changes and I call notifyItemInserted
. RecyclerView 会很好地显示所有内容,直到我的数据集发生更改并且我调用notifyItemInserted
。 New item is indeed inserted in the RecyclerView, but it is displayed without my custom divider (dividers for the other items are in place).新项目确实插入到 RecyclerView 中,但显示时没有我的自定义分隔符(其他项目的分隔符已就位)。
When I scroll away from the newly inserted item and then return back to it - it appears with the divider in place.当我从新插入的项目滚动离开然后返回到它时 - 它显示为分隔符就位。
EDIT Code snippets:编辑代码片段:
mRecyclerView.addItemDecoration(new GapDividerItemDecoration(mContext, LinearLayoutManager.VERTICAL, 16));
My custom decorator (just adds space between RecyclerView items provided in dp):我的自定义装饰器(只是在 dp 中提供的 RecyclerView 项目之间添加空间):
public class GapDividerItemDecoration extends DividerItemDecoration {
private int mSpaceInPixels;
private int mOrientation;
public GapDividerItemDecoration(Context context, int orientation, float dp) {
super(context, orientation);
mSpaceInPixels = dpToPixels(dp);
mOrientation = orientation;
}
private int dpToPixels(float dp) {
return (int) (dp * (((float) Resources.getSystem().getDisplayMetrics().densityDpi) / DisplayMetrics.DENSITY_DEFAULT));
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
// Do not draw the divider
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1) {
if (mOrientation == LinearLayout.VERTICAL) {
outRect.bottom = mSpaceInPixels;
} else {
outRect.right = mSpaceInPixels;
}
}
}
I do not provide the rest of the code, because it is pretty straight forward (besides, I know that it is working, and the issue might be with the RecyclerView
itself or just my custom decorator).我不提供其余的代码,因为它非常简单(此外,我知道它正在工作,问题可能出在RecyclerView
本身或只是我的自定义装饰器)。
This is a too late answer, but I will answer because I have the same problem.这是一个为时已晚的答案,但我会回答,因为我有同样的问题。 Try call RecyclerView.invalidateItemDecorations() before insert data.在插入数据之前尝试调用 RecyclerView.invalidateItemDecorations()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.