在 RecyclerView 中添加时使用路径创建问题的绘制形状

Drawn shape using path creating issue when added in RecyclerView

我创建了自定义视图并尝试在绘制时绘制形状。形状以青色显示在附件中。但是当我在滚动其原始宽度和重叠视图后在回收站视图中添加该自定义视图时。如果我尝试使用 line 它不会产生任何问题,但只有路径会产生问题。请建议一种方法来实现我想要使用的形状。

我用来在 onDraw() 方法中绘制形状的代码

            int totalWidth=xMargin+width;
            int totalHeight=yMargin+height;
            //draw required share here
            path.moveTo(totalWidth,yMargin);
            path.lineTo(totalWidth,yMargin);
            path.lineTo(totalWidth-offset,yMargin+offset);
            path.lineTo(totalWidth-offset,totalHeight-offset);
            path.lineTo(totalWidth-2*offset,totalHeight);
            path.lineTo(xMargin,totalHeight);
            path.lineTo(xMargin,yMargin+offset);
            path.lineTo(xMargin+offset,yMargin);

            canvas.drawPath(path, pathPaint);

你可以看到当我滚动时,我的绘图形状正在改变。

根据提供的少量信息,我可以假设 RecyclerView 未正确评估您的形状(聊天气泡),因为它可能不知道列表中每个项目的形状 type/size,这会导致你的清单碰撞一些项目。 考虑到这一点,我可以说我遇到了类似的问题并使用这些行解决了:

    // Invalidate data
    adapter.notifyDataSetChanged();
    // Hard reset of the list
    recyclerView.setAdapter(adapter);

通过上面的代码,我们重置了适配器以使数据集无效,迫使回收器丢弃所有项目视图并重新创建新视图(现在每个项目都有正确的视图)。

希望对您有所帮助。

由于路径未重置,因此它正在绘制重叠的绘图部分。 通过增加 path.reset();path.moveTo(totalWidth,yMargin); 我的问题解决之前。