在使用onSwipe时考虑RecyclerView中的边距空间

时间:2019-05-29 22:00:00

标签: java android android-recyclerview

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

enter image description here

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

enter image description here

循环视图布局

<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>

删除代码

以下是设置删除项目范围的代码:

@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;
    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, itemView.getTop(), itemView.getRight(), itemView.getBottom());
    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);


}

我需要调整什么才能避免边距重叠?

编辑:

回收器查看项目

<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

enter image description here

1 个答案:

答案 0 :(得分:1)

空白不是空白,而是TextView的空白。

所以,您可以这样做

@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);


}

当然,此解决方案仅在与您的特定商品布局配合使用时才有效