gpt4 book ai didi

Android - ItemDecoration 在添加项目或滚动时有时会消失或错位

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:44:32 29 4
gpt4 key购买 nike

我刚刚开始使用 ItemDecoration。我正在尝试在由 StaggeredGridLayoutManager 管理的两列 RecyclerView 中间实现分隔线。

这是我的 ItemDecoration 代码:

public class LobbyItemDecoration extends RecyclerView.ItemDecoration {

private int margin;
private Drawable drawable;

public LobbyItemDecoration(int margin, Drawable drawable){
this.margin = margin;
this.drawable = drawable;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
int position = parent.getChildAdapterPosition(view);

StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams)view .getLayoutParams();
int spanIndex = lp.getSpanIndex();

if(position >= 0){
if (position < ((LobiAdapter)parent.getAdapter()).getmDataset().size()){
if(spanIndex == 1){
outRect.left = margin;
((LobiAdapter)parent.getAdapter()).getmDataset().get(position).left = false;
} else {
outRect.right = margin;
((LobiAdapter)parent.getAdapter()).getmDataset().get(position).left = true;
}
outRect.bottom = margin;
}
}
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (drawable == null) { super.onDrawOver(c, parent, state);return; }

if(parent.getItemAnimator() != null && parent.getItemAnimator().isRunning()) {
return;
}

final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();

for (int i=1; i < childCount; i++) {
final View child = parent.getChildAt(i);
StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams)child .getLayoutParams();
int spanIndex = lp.getSpanIndex();
int size = drawable.getIntrinsicWidth();
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int ty = (int)(child.getTranslationY() + 0.5f);
final int tx = (int)(child.getTranslationX() + 0.5f);
final int leftIndex1 = child.getLeft() - (margin * 4 / 3) - tx;
final int rightIndex1 = child.getLeft() - (margin * 2 / 3) - tx;
final int leftIndex0 = child.getRight() + (margin * 2 / 3) + tx;
final int rightIndex0 = child.getRight() + (margin * 4 / 3) + tx;
if(spanIndex == 1){
// drawable.setBounds(100, top, 5, bottom);
drawable.setBounds(leftIndex1, top, rightIndex1, bottom);
drawable.draw(c);
} else {
drawable.setBounds(leftIndex0, top, rightIndex0, bottom);
drawable.draw(c);
// drawable.setBounds(5, top, 100, bottom);
}
}
}
}

问题如标题所述,每当我加载新项目或滚动时,装饰有时会消失或放错地方。我所说的消失的意思是,字面上消失了,直到我向下或向上滚动以回收 View 时,它才存在。

放错位置的意思是,当我向下滚动时,加载 ViewHolder 位于左列中,分隔线“粘”在左列中。如果加载的ViewHolder在右列,则正常。

以防万一:我使用 ViewHolder 作为进度指示器,方法是添加一个空项目并在 getItemViewType() 中检查它,然后在完成加载后将其删除来自服务器。

这是我添加的方式:

for (PostModel postModel :
dataSet) {
this.mDataset.add(postModel);
notifyItemInserted(mDataset.size() - 1);
}

最佳答案

StaggeredGridLayoutManager 有问题。对我来说,这有帮助:

  diffResult.dispatchUpdatesTo(this);
recyclerView.postDelayed(() -> {
layoutManager.invalidateSpanAssignments(); // to fix first item in second column issue
recyclerView.invalidateItemDecorations(); // to fix wrong spacing
}, 50);

关于Android - ItemDecoration 在添加项目或滚动时有时会消失或错位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39564769/

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