7

我已经实施了滑动以关闭我的回收站视图。它触发2个功能(记录的编辑和删除)。如果向左滑动,则编辑它,如果向右滑动,则将其删除。我在 recyclerview 项目的一般部分下有这样的布局

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fe5722">

    <ImageView
        android:id="@+id/edit_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="24dp"
        android:src="@mipmap/ic_mode_edit_white_24dp" />

    <ImageView
        android:id="@+id/delete_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="24dp"
        android:src="@mipmap/ic_delete_white_24dp" />
</RelativeLayout>

因此,如果您向左滑动(删除项目),左侧图标(用于编辑)应该隐藏到滑动结束。但是,我不知道如何在 ItemTouchHelper.SimpleCallback 类中获取滑动方向。请帮忙。

RecyclerItemTouchHelper.java

public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {
private RecyclerItemTouchHelperListener listener;
private ImageView imageView;

public RecyclerItemTouchHelper(int dragDirs, int swipeDirs, RecyclerItemTouchHelperListener listener) {
    super(dragDirs, swipeDirs);
    this.listener = listener;
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    return true;
}


@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
    if (viewHolder != null) {
        final View foregroundView = ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getLayout();
        getDefaultUIUtil().onSelected(foregroundView);
    }
}

@Override
public void onChildDrawOver(Canvas c, RecyclerView recyclerView,
                            RecyclerView.ViewHolder viewHolder, float dX, float dY,
                            int actionState, boolean isCurrentlyActive) {
    final View foregroundView = ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getLayout();
    getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY,
            actionState, isCurrentlyActive);
}

@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    final View foregroundView = ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getLayout();
    getDefaultUIUtil().clearView(foregroundView);
}

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView,
                        RecyclerView.ViewHolder viewHolder, float dX, float dY,
                        int actionState, boolean isCurrentlyActive) {
    ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).setEditIconInvisible();
    final View foregroundView = ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getLayout();

    getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,
            actionState, isCurrentlyActive);
}

@Override
public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {

    return super.getSwipeDirs(recyclerView, viewHolder);
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    listener.onSwiped(viewHolder, direction, viewHolder.getAdapterPosition());
}

@Override
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
    if(layoutDirection == ItemTouchHelper.LEFT){
        Log.d("myLogs", "Налево пошло");
    }
    return super.convertToAbsoluteDirection(flags, layoutDirection);
}

public interface RecyclerItemTouchHelperListener {
    void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position);
}

}

更新:我已经解决了这个问题,只是更新了 onChildDraw 方法:

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView,
                        RecyclerView.ViewHolder viewHolder, float dX, float dY,
                        int actionState, boolean isCurrentlyActive) {
    final View foregroundView = ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getLayout();
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        if (dX > 0) {
            ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getEditIcon().setVisibility(View.VISIBLE);
            ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getDeleteIcon().setVisibility(View.GONE);
        } else {
            ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getEditIcon().setVisibility(View.GONE);
            ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getDeleteIcon().setVisibility(View.VISIBLE);
        }
    }

    getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,
            actionState, isCurrentlyActive);
}
4

1 回答 1

6

ItemTouchHelper.Callback有一个公共方法onChildDraw(),将在每个帧上调用一个dX值。此值是由于用户执行当前滑动操作而导致的视图的水平平移。

您可以使用 的值dX(以及在之前为零时它现在是正数,还是在之前为零时现在是负数)来检测用户是从左到右还是从右到左滑动。

private class MyItemTouchCallback extends ItemTouchHelper.Callback {

    private float previousDx = 0;

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        ...
        previousDx = 0;
    }

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        if (previousDx <= 0 && dX > 0) {
            // swiping from left to right
        }
        else if (previousDx >= 0 && dX < 0) {
            // swiping from right to left
        }

        ...

        previousDx = dX;
    }
}
于 2018-02-14T21:28:57.683 回答