Java 使用onSwipe时计算RecyclerView中的边距空间

Java 使用onSwipe时计算RecyclerView中的边距空间,java,android,android-recyclerview,Java,Android,Android Recyclerview,我有一个显示姓名的RecyclerView。看起来像这样 @Override public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { super.onChildDraw

我有一个显示姓名的RecyclerView。看起来像这样

@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

    View itemView = viewHolder.itemView;
    //**********************
    TextView txt = itemView.findViewById(R.id.tv_usernameStatsAdapter);
    //*******************************
    int itemHeight = itemView.getHeight();

    boolean isCancelled = dX == 0 && !isCurrentlyActive;

    if (isCancelled) {
        clearCanvas(c, itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        return;
    }

    mBackground.setColor(backgroundColor);
    // ****************** 
    mBackground.setBounds(itemView.getRight() + (int) dX + txt.getPaddingStart(), itemView.getTop() + txt.getPaddingTop(), itemView.getRight() - txt.getPaddingEnd(), itemView.getBottom() - txt.getPaddingBottom());
    //**************************
    mBackground.draw(c);

    int deleteIconTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2;
    int deleteIconMargin = (itemHeight - intrinsicHeight) / 2;
    int deleteIconLeft = itemView.getRight() - deleteIconMargin - intrinsicWidth;
    int deleteIconRight = itemView.getRight() - deleteIconMargin;
    int deleteIconBottom = deleteIconTop + intrinsicHeight;


    deleteDrawable.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom - 6);
    deleteDrawable.draw(c);

    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);


}

项目之间的空白是边距。我遵循了一个指南,允许用户滑动以删除名称。唯一的问题是,删除时红色背景与边距重叠,我只希望它与蓝色项目相等。这个Gif更好地解释了这一点:

Recyler视图布局

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myCoordinatorLayoutStats"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".StatisticsActivity">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_playerNamesStats"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_selectPlayerStats" />

    <TextView
        android:id="@+id/tv_selectPlayerStats"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:gravity="center"
        android:text="@string/selectPlayerStats"
        android:textAlignment="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
我需要调整什么以避免边缘重叠

编辑:

Recyler查看项目

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/tv_usernameStatsAdapter"
    style="@style/users_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent"
    android:padding="8dp"
    android:maxLines="1"
    android:textColor="@color/whiteText"/>
</LinearLayout>

更新的gif


空白不是页边空白,而是
文本视图的填充

所以,你可以这样做

@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

    View itemView = viewHolder.itemView;
    //**********************
    TextView txt = itemView.findViewById(R.id.tv_usernameStatsAdapter);
    //*******************************
    int itemHeight = itemView.getHeight();

    boolean isCancelled = dX == 0 && !isCurrentlyActive;

    if (isCancelled) {
        clearCanvas(c, itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        return;
    }

    mBackground.setColor(backgroundColor);
    // ****************** 
    mBackground.setBounds(itemView.getRight() + (int) dX + txt.getPaddingStart(), itemView.getTop() + txt.getPaddingTop(), itemView.getRight() - txt.getPaddingEnd(), itemView.getBottom() - txt.getPaddingBottom());
    //**************************
    mBackground.draw(c);

    int deleteIconTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2;
    int deleteIconMargin = (itemHeight - intrinsicHeight) / 2;
    int deleteIconLeft = itemView.getRight() - deleteIconMargin - intrinsicWidth;
    int deleteIconRight = itemView.getRight() - deleteIconMargin;
    int deleteIconBottom = deleteIconTop + intrinsicHeight;


    deleteDrawable.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom - 6);
    deleteDrawable.draw(c);

    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);


}

当然,此解决方案仅在与特定项目布局一起使用时有效

为RecyclerView添加xml布局item@RasoolGhana我们越来越近了,现在有一点顶部被切断了。我将发布更新结果的GIF,但我已经比以前更喜欢这种方式了。