Android - 在添加项目或滚动时,ItemDecoration有时会消失或放错地方

Kev*_*vie 9 android

我刚刚开始使用ItemDecoration.我试图在一个由两个列RecyclerView管理的中间实现一个分隔符StaggeredGridLayoutManager.

这是我的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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

错误的意思是,当我向下滚动,并且加载ViewHolder在左列时,分隔符"粘住"到左列.如果装载ViewHolder在右栏中,则是正常的.

以防万一:我使用a ViewHolder作为进度指示器,添加一个空项并检查它,getItemViewType()然后在从服务器完成加载后将其删除.

这是我添加的方式:

for (PostModel postModel :
        dataSet) {
    this.mDataset.add(postModel);
    notifyItemInserted(mDataset.size() - 1);
}
Run Code Online (Sandbox Code Playgroud)

小智 3

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

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